【发布时间】:2013-08-22 23:47:37
【问题描述】:
在cplusplus.com reference 中指出,在尝试生成随机数时使用模运算符会使较小的数字更有可能:
random_var = rand() % 100 + 1; //this will generate numbers between 1-100
为什么较低的数字更有可能?如果是,我们为什么不使用下面的代码:
random_var = rand()/(RAND_MAX/100) + 1; //also will generate those, more uniform I guess
【问题讨论】:
-
我稍微解释了一下in this answer。
-
最简单的替换就是使用
<random>。