title: Random number generation - code not working as it should
body: <p>So I have to generate a random number (called 'p' here) between 0 and 90 whose frequency distribution is a cosine function (i.e I should have more numbers between 0 and 45 than numbers between 45 and 90).</p>
  
  <p>I am working on matlab
  The code is as follows -</p>
  
  <pre><code>flag = 1; 
  while flag == 1 
      candidate = randi([0,90]);
      if rand &lt; cosd( candidate )
          p = candidate;
          flag = 2;
      end
  end  
  </code></pre>
  
  <p>I am generating 20 such numbers but always I get most of the numbers towards the higher end (45-90).
  From those 20 numbers, there is hardly 1-2 numbers &lt; 45.</p>
  
  <p>Is my code wrong? </p>
  
  <p>EDIT: Okay, so I got the answer. I tried running the code separately as follows-</p>
  
  <pre><code>for i = 1:20
    flag = 1; 
    while flag == 1 
        candidate = randi([0,90]);
        if rand &lt; cosd( candidate )
            p = candidate;
            flag = 2;
            disp(p);
        end
    end  
  end
  </code></pre>
  
  <p>And I'm getting most of the values of p between 0 and 45. My original code had an external 'if' condition which was the reason for only accepting higher values of 'p'. I used a while loop and the number of iterations were much more than 20 to get 20 values of 'p'.</p>
  
  <p>Here is my original code snippet - </p>
  
  <pre><code>while zz &lt;=20 
          d = randi([0,359]);
          flag = 1;
  
          while flag == 1 
              c = randi([0,90]);
              x = rand(1);
              if x &lt; cosd(c)
                  p = c;
                  flag = 2;
              end
          end
  
  
  
          if 'external condition' 
                strike(zz) = d;
                dip(zz) = p;
                slip(zz) = round(i);
                zz= zz+1;
          end
  
  end
  </code></pre>