由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - random number generator in C++
相关主题
C++ rand()函数每次生成同一个数[合集] C++如何产生很大范围的随机数?
multiple random number generator随机数发生器问题请教
[合集] C++ STL questionhelp understanding code (random number)
How to generate random number in driver (build in DDK)Help! Read random number of lines in a input file.
ask a C question about random number问一个随机排列的问题.
请教一个c++概率小程序c++里如何产生一个standard normal sample
Random number generator in C++ 如何编程实现以下简单的组合问题
请教个C++程序设计[合集] 面试问题
相关话题的讨论汇总
话题: lowest话题: random话题: highest话题: rand话题: range
进入Programming版参与讨论
1 (共1页)
a*****n
发帖数: 5158
1
i want to generate 100 random numbers between 0-1,
why the following program only generate 100 SAME numbers?
#include
#include
float random(int lowest, int highest)
//return a random number between highest and lowest
{
srand(time(NULL));
float result=0.0;
int range=(highest-lowest)+1;
if(highest<=lowest)
return 0.0;
else {
result = float(lowest)+range*rand()/(RAND_MAX + 1.0);
return result;
}
h********l
发帖数: 67
2
try this
a*****n
发帖数: 5158
3
why remove +1.0?
it will change range from (lowest,highest] to (lowest,highest)
by the way, if i remove the 1.0, then i can only get
a same integer, don't know why

【在 h********l 的大作中提到】
: try this
a*****n
发帖数: 5158
4
it is strange,
it works fine if i don't use the function (i.e., just
put all these into main()

【在 a*****n 的大作中提到】
: i want to generate 100 random numbers between 0-1,
: why the following program only generate 100 SAME numbers?
: #include
: #include
: float random(int lowest, int highest)
: //return a random number between highest and lowest
: {
: srand(time(NULL));
: float result=0.0;
: int range=(highest-lowest)+1;

h********l
发帖数: 67
5
the problem is in the following statement:
"srand(time(NULL))"
The program resets the random seed for each random number.
But the seed (time(NULL)) doesn't change for 100 numbers as the program runs
so quickly. So the program prints out 100 same numbers.
Moving "srand(time(NULL))" to the main function will solve the problem.

【在 a*****n 的大作中提到】
: it is strange,
: it works fine if i don't use the function (i.e., just
: put all these into main()

O*******d
发帖数: 20343
6
These lines have problem. You will not get random numbers between lowest
and highest. You will, instead, get random numbers between lowest inclusive
and highest+1 exclusive. Take example highest = 3, lowest = 2, then range
is 2. The lower boundary of random number is 2.0 + 2.0 * 0.0 = 2.0. The
upper boundary of random number is 2.0 + 2.0 * 1.0 = 4.0. Because rand()/(
RAND_MAX + 1.0) can never be 1.0, the random numbers generated have upper
boundary of 4.0 exclusive.
int range=(highest-low
D*******a
发帖数: 3688
7
把srand()提出去

【在 a*****n 的大作中提到】
: i want to generate 100 random numbers between 0-1,
: why the following program only generate 100 SAME numbers?
: #include
: #include
: float random(int lowest, int highest)
: //return a random number between highest and lowest
: {
: srand(time(NULL));
: float result=0.0;
: int range=(highest-lowest)+1;

a*****n
发帖数: 5158
8
也不行,
必须要将srand与rand都入在main()里才行,真是见鬼
难道没调用一次random(),不要再运行srand?

【在 D*******a 的大作中提到】
: 把srand()提出去
c*s
发帖数: 5
9
cout << rand()%100/100. << endl;
cout << rand()%1000/1000. << endl;
...
depends on how many digits you want the accuracy.

【在 a*****n 的大作中提到】
: i want to generate 100 random numbers between 0-1,
: why the following program only generate 100 SAME numbers?
: #include
: #include
: float random(int lowest, int highest)
: //return a random number between highest and lowest
: {
: srand(time(NULL));
: float result=0.0;
: int range=(highest-lowest)+1;

O******e
发帖数: 734
10
You must not call the seeding function everytime you call the RNG.
As happyangel explained, your original program called srand multiple
times in the same second on the clock, and as a result you kept on
reseeding the RNG back to the same initial number.
Call srand only once in main, then call rand as many times as you
need. If you call rand frequently, then after some time you should
consider reseeding it. I usually reseed an RNG after having called
it to generate sqrt(RAND_MAX) many numbers.

【在 a*****n 的大作中提到】
: 也不行,
: 必须要将srand与rand都入在main()里才行,真是见鬼
: 难道没调用一次random(),不要再运行srand?

相关主题
请教一个c++概率小程序[合集] C++如何产生很大范围的随机数?
Random number generator in C++随机数发生器问题请教
请教个C++程序设计help understanding code (random number)
进入Programming版参与讨论
O******e
发帖数: 734
11
It's generally not a good idea to do this. See Knuth, or Numerical
Recipes, or the Linux man page of rand. The OP had the right idea.

【在 c*s 的大作中提到】
: cout << rand()%100/100. << endl;
: cout << rand()%1000/1000. << endl;
: ...
: depends on how many digits you want the accuracy.

O******e
发帖数: 734
12
If you want your random() to return an integer between lowest and highest
inclusively, then
range=highest-lowest+1;
random=lowest+(int)(range*rand()/(RAND_MAX+1.0));
But you want your random() to return a float between lowest and highest,
so you need
range=highest-lowest;
random=lowest+range*rand()/(RAND_MAX+1.0);

【在 a*****n 的大作中提到】
: why remove +1.0?
: it will change range from (lowest,highest] to (lowest,highest)
: by the way, if i remove the 1.0, then i can only get
: a same integer, don't know why

o******r
发帖数: 259
13

不用浮点运算
random = lowest + rand()%range;
(suppose lowest, highest都是 整数)

【在 O******e 的大作中提到】
: If you want your random() to return an integer between lowest and highest
: inclusively, then
: range=highest-lowest+1;
: random=lowest+(int)(range*rand()/(RAND_MAX+1.0));
: But you want your random() to return a float between lowest and highest,
: so you need
: range=highest-lowest;
: random=lowest+range*rand()/(RAND_MAX+1.0);

a*****n
发帖数: 5158
14
thanks, very clear now

【在 O******e 的大作中提到】
: You must not call the seeding function everytime you call the RNG.
: As happyangel explained, your original program called srand multiple
: times in the same second on the clock, and as a result you kept on
: reseeding the RNG back to the same initial number.
: Call srand only once in main, then call rand as many times as you
: need. If you call rand frequently, then after some time you should
: consider reseeding it. I usually reseed an RNG after having called
: it to generate sqrt(RAND_MAX) many numbers.

a*****n
发帖数: 5158
15

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
你这个是lowest inclusive的八? i mean range is
[lowest, highest), how to get (lowest,highest)?

【在 O******e 的大作中提到】
: If you want your random() to return an integer between lowest and highest
: inclusively, then
: range=highest-lowest+1;
: random=lowest+(int)(range*rand()/(RAND_MAX+1.0));
: But you want your random() to return a float between lowest and highest,
: so you need
: range=highest-lowest;
: random=lowest+range*rand()/(RAND_MAX+1.0);

O******e
发帖数: 734
16
The earlier example was inclusive at both ends.
If you want to exclude both ends, then just use lowest++ and highest--.
I think it is more natural than an integer random() return values that
are inclusive of the end points. When you roll a die, you always
say that the die will show "1 to 6 dots", not "0 to 7 dots exclusive
of the lower and upper bounds", right?

【在 a*****n 的大作中提到】
:
: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
: 你这个是lowest inclusive的八? i mean range is
: [lowest, highest), how to get (lowest,highest)?

g***p
发帖数: 139
17
ft,一样的seed,你的random将产生一组一样的随机数。
每一次你call func,就得到一组一样的随机数的第一个。

【在 a*****n 的大作中提到】
: it is strange,
: it works fine if i don't use the function (i.e., just
: put all these into main()

1 (共1页)
进入Programming版参与讨论
相关主题
[合集] 面试问题ask a C question about random number
c++产生随机数请教一个c++概率小程序
[合集] 为什么多个线程生成的随机数是一样的?Random number generator in C++
方块里面的随机位置请教个C++程序设计
C++ rand()函数每次生成同一个数[合集] C++如何产生很大范围的随机数?
multiple random number generator随机数发生器问题请教
[合集] C++ STL questionhelp understanding code (random number)
How to generate random number in driver (build in DDK)Help! Read random number of lines in a input file.
相关话题的讨论汇总
话题: lowest话题: random话题: highest话题: rand话题: range