title: For loop and While loop to select values in C
body: <p>Could someone help me to look at the code I wrote in C? It can generate numbers, but the if condition looks like wrong, but I donot know how to fix it.</p>
  
  <p>I need to get some n random values with distribution and these values are in a special intervals, so I use for loop like below: </p>
  
  <pre><code>  n=10; mu=2; mu_plus=3; p=0.2;
  
    for(i=1; i&lt;=n; i++)
      {
      x1 = gasdev(&amp;idum);
      x2 = gasdev(&amp;idum);
      z  = sqrt(1-p*p)*x1 + p*x2 + mu;
      z_p= x2 + mu_plus;
  
      if (z &gt; 2.17 || z&lt;-2.17) 
       {   
          Z[i]=z; 
          Z_plus[count]=z_p;
       }
    printf("%d %lf %lf\n", i, Z[i], Z_plus[i]);   
  }
  </code></pre>
  
  <p>Where gasdev() is a function for generating random value with a standard normal distribution, Z and Z_plus are 1*n vectors. The results are mess, so I think that the IF condition must be wrong. Could any one could help me? Thank you.</p>
  
  <p>I also tried the While loop.</p>
  
  <pre><code>   while(count &lt; n)
      {
        x1 = gasdev(&amp;idum);
        x2 = gasdev(&amp;idum);
        z  = sqrt(1-p*p)*x1 + p*x2 + mu;
        z_p= x2 + mu_plus;
  
     if (z &gt; 2.17 || z&lt;-2.17) 
     {
         count++;         
         Z[count]=z; 
          Z_plus[count]=z_p;
      }
      printf("%d %lf\n",count, Z[count]);
  
      if (count &gt;n) break;
    }
  </code></pre>
  
  <p>It can print out normally, but occurs an error at the end.</p>
  
  <p>Thank you!</p>