error C2296: '%' : illegal, left operand has type 'double'
when I try rand() % 1.0
Simple rand() question
I can't figure out the proper way to get a random number between 0.0 and 1.0 (ie 0.1, 0.9, 0.4). I keep getting
Do this:
You can't use the modulo operator on a float.
Also, just wanted to point out that the above is the most varied possible value of 0-1 you can get via use of the rand() function.
[Edited by - silverphyre673 on June 2, 2007 12:25:40 PM]
float zero_to_one = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
You can't use the modulo operator on a float.
Also, just wanted to point out that the above is the most varied possible value of 0-1 you can get via use of the rand() function.
[Edited by - silverphyre673 on June 2, 2007 12:25:40 PM]
You are getting an error because the modulus operator (%) can only be used with integers.
rand returns a random integer. In order to obtain a decimal number between 0.0 and 1.0, you need something like the following:
For a more random sample, use a larger number to apply the modulus with:
When you apply %1001 to rand(), it returns integer values between 0 and 1000. Then you simply convert it to a float and divide by 1000.0f to get values between 0.0 and 1.0. Mathematically speaking, the formula is:
(rand()%(10^n + 1)) / 10^n
The larger the value of n, the more varied the random numbers will be.
rand returns a random integer. In order to obtain a decimal number between 0.0 and 1.0, you need something like the following:
float num = (float)(rand()%11) / 10.0f;
For a more random sample, use a larger number to apply the modulus with:
float num = (float)(rand()%1001) / 1000.0f;
When you apply %1001 to rand(), it returns integer values between 0 and 1000. Then you simply convert it to a float and divide by 1000.0f to get values between 0.0 and 1.0. Mathematically speaking, the formula is:
(rand()%(10^n + 1)) / 10^n
The larger the value of n, the more varied the random numbers will be.
Quote:Original post by silverphyre673
Do this:float zero_to_one = static_cast<float>(rand()) / static_cast<float>(RAND_MAX);
You can't use the modulo operator on a float.
Also, just wanted to point out that the above is the most varied possible value of 0-1 you can get via use of the rand() function.
Well when I use this all I get is 0.65xxxx every time, so that's not very random when all I need is a number between 0-9 in the one's digit. Hah, but thank you
You need to seed the random number generator with the srand() function before using the rand() function to avoid getting the same number sequence everytime.
generally, you would do something like:
srand(time(0));
or
srand(GetTickCount());
generally, you would do something like:
srand(time(0));
or
srand(GetTickCount());
Quote:Original post by argonaut
You need to seed the random number generator with the srand() function before using the rand() function to avoid getting the same number sequence everytime.
generally, you would do something like:
srand(time(0));
or
srand(GetTickCount());
And just to avoid a common abuse of srand: you only need to call srand once in your program, and not before every call to rand.
Perost makes a good point. Another thing maybe worth mentioning: I typically don't seed the random generator until I am certain all my other algorithms are working correctly, since random numbers can make debugging harder to do.
If you need a random number between 0 and 1 with one decimal number, use:
(float)(rand()%11) / 10.0f;
(float)(rand()%11) / 10.0f;
Quote:Original post by Chaotic_Attractor
If you need a random number between 0 and 1 with one decimal number, use:
(float)(rand()%11) / 10.0f;
Actually, silverphyre673's way is better, you'll get much better results. Just seed it with srand() like others have said.
Quote:Original post by argonaut
You need to seed the random number generator with the srand() function before using the rand() function to avoid getting the same number sequence everytime.
generally, you would do something like:
srand(time(0));
or
srand(GetTickCount());
I did seed the rand()...and I consistently got a number .65xxxxx.
So tried Chaotic Attractor's suggestion of (float)(rand()%11)/10f and it works just how I wanted, although I don't know what difference the 'f' makes after the 10.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement