在C ++中生成随机数的最好方法是什么? [英] What is the best way to generate random numbers in C++?
问题描述
生成随机数的最佳方法是什么?
> 您没有C ++ 11支持,甚至没有TR1(因此您没有其他选择)
然后,您可以考虑使用以下C风格的解决方案,(为了这个社区的声誉,请参阅 rand()Considered有害)以透明字体写成:
这里是简单的C风格函数,它从 min
到 max
(包括)的间隔生成随机数。这些数字似乎非常接近均匀分布。
int irand(int min,int max){
return((double)rand()/((double)RAND_MAX + 1.0))*(max - min + 1)+ min;
}
,不要忘记呼叫 srand
之前使用它:
int occurences [8] = {0} ;
srand(time(0));
for(int i = 0; i <100000; ++ i)
++出现[irand(1,7)];
for(int i = 1; i <= 7; ++ i)
printf(%d,occurences [i]);
输出: 14253 14481 14210 14029 14289 14503 14235
另请参阅:
在范围内生成一个随机数?
在整个范围内生成随机数字
,并找到一些时间,并至少观察前11分钟的上述 ://en.cppreference.com/w/cpp/numeric/randomrel =nofollow> < random>
已通过 Kerrek SB 。
What is the best way to generate random numbers?
解决方案 If and only if:
you are not looking for "perfect uniformity" or
you have no C++11 support and not even TR1 (thus you don't have another choice)
then you might consider using the following C-style solution, which (for the sake of the reputation of this community ~ see rand() Considered Harmful) is written in strike-through font:
Here's the simple C-style function that generates random number from the interval from min
to max
, inclusive. Those numbers seem to be very close to being uniformly distributed.
int irand(int min, int max) {
return ((double)rand() / ((double)RAND_MAX + 1.0)) * (max - min + 1) + min;
}
and don't forget to call srand
before you use it:
int occurences[8] = {0};
srand(time(0));
for (int i = 0; i < 100000; ++i)
++occurences[irand(1,7)];
for (int i = 1; i <= 7; ++i)
printf("%d ", occurences[i]);
output: 14253 14481 14210 14029 14289 14503 14235
Also have a look at:
Generate a random number within range?
Generate random numbers uniformly over an entire range
and find some time and watch at least first 11 minutes of aforementioned video
Otherwise:
use <random>
just like it was pointed out by Kerrek SB already.
这篇关于在C ++中生成随机数的最好方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
min
到 max
(包括)的间隔生成随机数。这些数字似乎非常接近均匀分布。 int irand(int min,int max){
return((double)rand()/((double)RAND_MAX + 1.0))*(max - min + 1)+ min;
}
srand
之前使用它: int occurences [8] = {0} ;
srand(time(0));
for(int i = 0; i <100000; ++ i)
++出现[irand(1,7)];
for(int i = 1; i <= 7; ++ i)
printf(%d,occurences [i]);
14253 14481 14210 14029 14289 14503 14235
在范围内生成一个随机数?
在整个范围内生成随机数字
,并找到一些时间,并至少观察前11分钟的上述 ://en.cppreference.com/w/cpp/numeric/randomrel =nofollow>
< random>
已通过 Kerrek SB 。you are not looking for "perfect uniformity" or
you have no C++11 support and not even TR1 (thus you don't have another choice)
min
to max
, inclusive. Those numbers seem to be very close to being uniformly distributed.int irand(int min, int max) {
return ((double)rand() / ((double)RAND_MAX + 1.0)) * (max - min + 1) + min;
}
srand
before you use it:int occurences[8] = {0};
srand(time(0));
for (int i = 0; i < 100000; ++i)
++occurences[irand(1,7)];
for (int i = 1; i <= 7; ++i)
printf("%d ", occurences[i]);
14253 14481 14210 14029 14289 14503 14235
Generate a random number within range?
Generate random numbers uniformly over an entire range
and find some time and watch at least first 11 minutes of aforementioned video
<random>
just like it was pointed out by Kerrek SB already.