mercredi 11 mai 2016

Generate a random number less than 4 digits but the probability of it having 1, 2, or 3 digits is equal

I'm currently using 1 + (int)(rand() * 999.0 / RAND_MAX) to generate a random number between 1 and 999 inclusive but the two and one digit numbers don't occur as often as the three digit numbers.

How can I fix this?

Note that although the original code gives a range of 0 to 999 inclusive, I actually want a range of 1 to 999 inclusive.




Aucun commentaire:

Enregistrer un commentaire