dimanche 2 août 2015

Random number generation - code not working as it should

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).

I am working on matlab The code is as follows -

flag = 1; 
while flag == 1 
    candidate = randi([0,90]);
    if rand < cosd( candidate )
        p = candidate;
        flag = 2;
    end
end  

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 < 45.

Is my code wrong?




Aucun commentaire:

Enregistrer un commentaire