# Random Function

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

## Recommended Posts

What's the random function? Also, how should i make my own random function that takes in a Min, and Max and returns a value between the two?

##### Share on other sites
Programming language?

sry, C++

?

##### Share on other sites
The C standard random function is rand. On operating systems that support them, you should use random or rand48 instead. Generating random numbers within bounds is as easy as n = min + rand() % (max-min).

##### Share on other sites
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 on other sites
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 on other sites
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 on other sites
what about a random floating number between two floats? how would i figure that out?

##### Share on other sites
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 on other sites
Or just use boost::random.

##### Share on other sites
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 on other sites
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 on other sites
does it return an int or a float, if it returns a float then that's perfect

##### Share on other sites
It returns an int.

##### Share on other sites
so then how can i get something bewteen 0 and 1 if it returns an int?

##### Share on other sites
okkkk? how's this then:

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

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

##### Share on other sites
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 on other sites
Quote:
 Original post by EvilKnuckles666okkkk? 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 on other sites
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 on other sites
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 on other sites
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 on other sites
What the??????

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

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