Sign in to follow this  
BloodLust666

Random Function

Recommended Posts

Mercury    776
Quote:
Also, how should i make my own random function that takes in a Min, and Max and returns a value between the two?

int randomNumber = (rand() % (max-min)) + min;

Share this post


Link to post
Share on other sites
BloodLust666    104
doesn't rand() return a value between 0 and 1? (if not using srand(long) to define that max)

Here's what i came up with, is thie correct?

long GenerateRandom(long Min, long Max)
{
long Num;
srand(Max - Min);

Num = rand() + Min;
return Num;
}

Share this post


Link to post
Share on other sites
igni ferroque    415
srand sets the seed for the random number generator. The seed should be from a constantly changing source. A milli or microsecond counter would work. As stated in the manpage, rand returns an integer between 0 and RAND_MAX as defined in stdlib.h. Read the linked manpage for details.

Sidenote: Variable names begin with lowercase letters by convention.

Share this post


Link to post
Share on other sites
nobodynews    3126
step-by-step how to do it:

float RN = rand();
float result1 = RN / RAND_MAX;
float low = 2, high = 5;
float result1 = result1 * (high - low) + low;
cout << "result: " << result2 << endl;


one line way to do it(assuming high and low are defined floats:

float result = (rand()*1.0 / RAND_MAX) * (high - low) + low;


It is VERY important that rand() be converted into a floating point number in C++. If you have an integer divided by another integer it rounds so if you have, say rand() equals 10 then 10 / RAND_MAX will be zero. If you convert it to a float before hand then it will do floating point math and you won't lose any precision. The code is fairly straight forward, I hope.

Share this post


Link to post
Share on other sites
BloodLust666    104
but i thought rand() return an int of 0 to the max? if the max isn't set then it's assumed to be 1. so calling rand() i would either get 0 or 1.

someone correct me if i'm wrong

Share this post


Link to post
Share on other sites
SiCrane    11839
No, rand() returns a number between 0 and RAND_MAX where RAND_MAX is a pre-define preprocessor symbol set by the implementer of your runtime library.

Share this post


Link to post
Share on other sites
Glak    315
that won't give you a uniform distribution.

float RandomFloatBetween0And1()
{
return float(rand())/float(RAND_MAX);
}

int main()
{
srand(GetSystemTime());//use whatever the real time function is
cout<<RandomFloatBetween0And1();
return 0;
}

Share this post


Link to post
Share on other sites
Iftah    413
Quote:
Original post by EvilKnuckles666
okkkk? how's this then:

float GenerateRandomf(float Min, float Max)
{
float Num;
srand(100);

Num = ((1 / rand()) * (Max - Min)) + Min;
return Num;
}


you are reseting the seed to 100 each time --> you will get the same "random" number each time.

srand should be called only once, at the begining of the program and should be to a different seed each time (current time is the perfect choice)

Iftah.

Share this post


Link to post
Share on other sites
Omaha    100
What's a random function? Um, CreateWindow()! First one that came to mind.

Want another random function? sin()!

Sorry, I was born with a broken sarcasm governor.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Quote:
doesn't rand() return a value between 0 and 1? (if not using srand(long) to define that max)
You need to learn to read documentation, particularly when it is spoon-fed to you.

Share this post


Link to post
Share on other sites
BloodLust666    104
What's wrong with this one now?? sry, i'm doing so much stuff that i'm trying to get everythign done so quickly...

float GenerateRandomf(float Min, float Max)
{
float Num;

Num = ((rand() / timeGetTime()) * (Max - Min)) + Min;
return Num;
}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
What the??????

Where did you get that formula from? Why are you dividing by the current time??

Share this post


Link to post
Share on other sites
BloodLust666    104
i don't know, i orginally had (1 / rand()) but i'm trying to get it to work fast so i just thought of somethign else.

edit: gee thanks, instead of just critisizing my guesses to the problem, y not at LEAST leave some help if you know what ur doing.

Share this post


Link to post
Share on other sites
stylin    758
Quote:
Original post by EvilKnuckles666
i don't know, i orginally had (1 / rand()) but i'm trying to get it to work fast so i just thought of somethign else.

edit: gee thanks, instead of just critisizing my guesses to the problem, y not at LEAST leave some help if you know what ur doing.

You need to slow down and try and figure out what's going on with your code. That involves reading the documentation, and digesting what helpful folks have posted for you. If it helps, you may want to try and write down, step-by-step what you want to accomplish on paper (I'm not talking about code, either), and then write down step-by-step what is happening in your code.

In this instance, read the documentation on rand(): mainly what it returns, and what you can do to affect the numbers it provides you.

Above all, take your time. Experimentation is great and can be very helpful, but haphazardly throwing together code is not going to help you, but merely work to frustrate you. Take the extra time and learn the information; it will be better for you in the long run.

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