Jump to content
  • Advertisement
Sign in to follow this  
Shakedown

Simple rand() question

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
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 this post


Link to post
Share on other sites
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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!