lundi 14 septembre 2015

rand()%X isn't fairly [duplicate]

This question already has an answer here:

I've made a little program that that stores the results from rand()%49+1 in an array. I've made a loop with 1 million times increment "(int i=0;i<1000000;i++)" and saw that the first array (the array that store the value of 1) is a bit more often than the last array(the array that stores the value of 49). This happens in 90% of the cases. The problem for this is that the RAND_MAX is 32767. 32767 Mod 49 is 35. That means that the numbers from 1 to 36 have more chances than the numbers from 37-49. So how can I make RAND_MAX to be a number whose Mod 49 will be 48(48+1 will result in 49 so all numbers from 1-49 will have same chances)?




Aucun commentaire:

Enregistrer un commentaire