Simple rand() question

Started by
11 comments, last by nobodynews 16 years, 10 months ago
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

error C2296: '%' : illegal, left operand has type 'double'
when I try rand() % 1.0
Advertisement
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.

[Edited by - silverphyre673 on June 2, 2007 12:25:40 PM]
my siteGenius is 1% inspiration and 99% perspiration
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:

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.
Strength without justice is no vice; justice without strength is no virtue.
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());

~Argonaut________________________________Why "~Argonaut"? It's all just a mathematical expression denoting a close approximation of "Argonaut", which is irrational and can't be precisely defined.
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.
~Argonaut________________________________Why "~Argonaut"? It's all just a mathematical expression denoting a close approximation of "Argonaut", which is irrational and can't be precisely defined.
If you need a random number between 0 and 1 with one decimal number, use:

(float)(rand()%11) / 10.0f;
Strength without justice is no vice; justice without strength is no virtue.
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.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

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