Public Group

# Simple rand() question

This topic is 4059 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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]

##### Share on other sites
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.

##### Share on other sites
Quote:
 Original post by silverphyre673Do this:float zero_to_one = static_cast(rand()) / static_cast(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

##### Share on other sites
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());

##### Share on other sites
Quote:
 Original post by argonautYou 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));orsrand(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.

##### Share on other sites
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.

##### Share on other sites
If you need a random number between 0 and 1 with one decimal number, use:

(float)(rand()%11) / 10.0f;

##### Share on other sites
Quote:
 Original post by Chaotic_AttractorIf 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.

##### Share on other sites
Quote:
 Original post by argonautYou 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));orsrand(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.

1. 1
Rutin
26
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 9
• 9
• 46
• 41
• ### Forum Statistics

• Total Topics
631750
• Total Posts
3002062
×