title: random number inside for loop
body: <p>This is driving me crazy already and by now I'm absolutely hopeless :(</p>
  
  <p>Here is the matrix <code>draw</code> (don't pay attention to NAs, later I need those columns):</p>
  
  <pre><code>      [,1] [,2] [,3] [,4] [,5]  
  [1,]    1    0   NA   NA   NA  
  [2,]    2   20   NA   NA   NA  
  [3,]    2   30   NA   NA   NA  
  [4,]    2   40   NA   NA   NA  
  [5,]    1   50   NA   NA   NA  
  [6,]    2   70   NA   NA   NA  
  [7,]    2   80   NA   NA   NA  
  [8,]    2   90   NA   NA   NA  
  [9,]    1  100   NA   NA   NA
  [10,]   2  120   NA   NA   NA
  [11,]   2  130   NA   NA   NA
  [12,]   2  140   NA   NA   NA
  </code></pre>
  
  <p>so, depending on column 1 I need to write number in column 3 from normal distribution. But in 6% of the cases it have to be 0.</p>
  
  <p>This is how I thought to deal with it (by now it's only when <code>draw[i,1]==2</code>):</p>
  
  <pre><code>for (i in 1:nrow(draw))
  {
      p &lt;- runif(1, min=0, max=1)
      if (draw[i,2]==2 &amp;&amp; p&gt;0.06) 
      {   
          draw[i,3] &lt;- rnorm(1, mean=17, sd=7.5)} else {draw[i,3]&lt;-0}
  
      if (draw[i,1]==1) {draw[i,3] &lt;- rnorm(1, mean=11, sd=3)}
      if (draw[i,1]==3) {draw[i,3] &lt;- rnorm(1, mean=17, sd=15)}
      if (draw[i,3]&lt;1 &amp;&amp; draw[i,3]!=0) {draw[i,3] &lt;- 1} else {draw[i,3] &lt;- ceiling(draw[i,3])
  }
  }
  </code></pre>
  
  <p>and there problems come:
   1. I notices p (which have to be random number I guess) is always the same
   2. all draw[i,3] where draw[1,2]==2 are 0. This is strange, because it happens even when p>0.06:</p>
  
  <pre><code>      [,1] [,2] [,3] [,4] [,5]
   [1,]    1    0   19   NA   NA
   [2,]    2   20    0   NA   NA
   [3,]    2   30    0   NA   NA
   [4,]    2   40    0   NA   NA
   [5,]    1   50   11   NA   NA
   [6,]    2   70    0   NA   NA
   [7,]    2   80    0   NA   NA
   [8,]    2   90    0   NA   NA
   [9,]    1  100    7   NA   NA
  [10,]    2  120    0   NA   NA
  [11,]    2  130    0   NA   NA
  [12,]    2  140    0   NA   NA
  </code></pre>
  
  <p>why?! what is wrong?</p>