changing probability of getting a random number

前端 未结 7 1535
南方客
南方客 2020-12-09 22:15

I would like to generate a random number between 0 and 3 and I have the following in my code:

int random = rand() % 4;

This works fine but

7条回答
  •  时光取名叫无心
    2020-12-09 22:39

    how many numbers have you tested this on? if it is actually true you can instead generate a range from say 0->3999 using a = rand()%4000 and use int = a/1000 this should remove the weight of the apparently under produced zero.

提交回复
热议问题