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 | |
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?
|
|
|
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()
|