Random Function

Started by
32 comments, last by eq 18 years, 6 months ago
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.
Advertisement
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;
}
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
What the??????

Where did you get that formula from? Why are you dividing by the current time??
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.
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
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.
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
all i'm looking for is the equation. then i'll study that. here's what i'm getting:

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

Num = ((rand() / RAND_MAX ) * (Max - Min)) + Min;
W_ErrorLog.WriteNumber("Rand(): ", rand(), 16);
W_ErrorLog.WriteFloat("Random: ", rand() / RAND_MAX, 32);
return Num;
}

my output is this:
Rand(): 9285
Random: 0.000000
Rand(): 31474
Random: 0.000000
Rand(): 28524
Random: 0.000000
Rand(): 208
Random: 0.000000
Rand(): 20522
Random: 0.000000
Rand(): 7417
Random: 0.000000
Rand(): 17299
Random: 0.000000
Rand(): 32304
Random: 0.000000
Rand(): 4511
Random: 0.000000
Rand(): 20475
Random: 0.000000
Rand(): 20488
Random: 0.000000
Rand(): 10267
Random: 0.000000
Rand(): 8004
Random: 0.000000
Rand(): 11988
Random: 0.000000
Rand(): 27429
Random: 0.000000
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
y the hell is it equaling 0.0????? (!@#$!@#'ing frestrated)
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
If you read the line of code someone already provided:

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

You see how they are multiplying rand() by 1.0? This is to convert it from an integer to a floating point value. You could also cast the result from rand() to a floating point value. Unless it is converted to a floating point value, it is an integer, and an integer divided by another integer uses integer arithmetic. Evaluating 1/2 or 654/5000 or anything like that will result in 0. Cast the random number to a float before dividing it by RAND_MAX to make it use floating point arithmetic.
thank you
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
how many times do u need people to post the same thing

This topic is closed to new replies.

Advertisement