avatar
random number generator in C++# Programming - 葵花宝典
a*n
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;
}
avatar
h*l
2
try this
avatar
a*n
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
avatar
a*n
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;

avatar
h*l
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()

avatar
O*d
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
avatar
D*a
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;

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

【在 D*******a 的大作中提到】
: 把srand()提出去
avatar
c*s
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;

avatar
O*e
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?

avatar
O*e
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.

avatar
O*e
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

avatar
o*r
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);

avatar
a*n
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.

avatar
a*n
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);

avatar
O*e
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)?

avatar
g*p
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()

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。