Skip to main content
gamedev.net
🔒 Locked

Random

Started by Link Oct 26, 2003 at 9:10 AM 8 replies 500+ views
Original Post
Link
Link
what is the function that return a random number using 2 values of lower possible value to bigger, for example int num = functionRandom(4,80); // call the function num = 45; //45 is between 4-80 Please get me all information to header to include and all needs.
dede
dede
I''ll assume your using C++, and I''ll assume it doesn''t really matter the ''goodness'' of you random numbers(i.e. game, rather than crypto or hardcore simulation).

const int low = 12;
const int high = 25;

int number = ((high-low)*rand()/MAX_RAND) + low;

There is another way too!

int number = rand() % (high-low) + low;


The top one produced slightly better random numbers, because the bias is closer to the center, the second one is faster. Either one is good, unless your using cryptography, you won''t ''feel'' the bias.

You need to seed rand(), howto is usually in rand()''s documentation.

~~~~~
"Sorry(dede), I''m only 22 years old. But I WOULD kill you if I see you around " - pouya.
Download and play Slime King I.
Link
Link
Thanks
cavemanbob
cavemanbob
Do NOT do as Raptor85 suggested, doing so will cause the output to be signifigantly less random.
iNfuSeD
iNfuSeD
with that random number generator using background radiation noise from a radio tuner.. i got an idea. if computers were to start manufacturing cases with a very very simple radio tuner, just enough to pick up static on any freq, and turn that into bits tied to the bios, then programs could take this as a random # seed at will. how hard would it be to tie a coil of wires on a nail into a simple hackjob of a modulator chip?
"The human mind is limited only by the bounds which we impose upon ourselves." -iNfuSeD
Thunder_Hawk
Thunder_Hawk
quote:
Original post by Beer Hunter
quote:
Original post by ApochPiQ
Just in case you want really hardcore random numbers, look into the Mersenne Twister algorithm.
Hey, if you want hardcore ...


Dear god, that took like 2 minutes to load when the query was a number between 1 and 10 (5).



______________________________________________________________
The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ
MySite
______________________________________________________________
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Forma

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.