Sign in to follow this  
Shakedown

Simple rand() question

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
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
It's like 10f, it tells the compiler it's a floating point number, not an int or a double. It's the same as 10.0f too just fyi which is how I normally do it (with the decimal) so I don't confuse it.

Share this post


Link to post
Share on other sites
Quote:

It's like 10f, it tells the compiler it's a floating point number, not an int or a double. It's the same as 10.0f too just fyi which is how I normally do it (with the decimal) so I don't confuse it.


I thought adding the decimal point was just as good as adding the f. So 10.0 was the same as 10f.

- Goishin

Share this post


Link to post
Share on other sites
Sure there's a difference. Consider this program:

#include <iostream>

float function(float alpha, float beta)
{
return alpha + beta;
}

double function(double alpha, double beta)
{
return alpha * beta;
}


int main()
{
std::cout << "If you multiply 10 and 20 you get: " << function(10.0, 20.0) << std::endl;
std::cout << "If you add 10 and 20 you get: " << function(10.0f, 20.0f) << std::endl;

}


If you didn't use the f-suffix you wouldn't be able to call the float verion of function. Which is to say that by default 10.0 is a double while 10.0f is a float. See this part of the draft standard.

Quote:
The type of a floating literal is double unless explicitly specified by a suffix.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this