🔒 Locked
Random
Advertisement
Original Post
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.
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
Download and play Slime King I.
If you want a random number between 4-80 you want a random number between 0-76 + 4 ie.
int Num = 4 + rand() % 76;
I think it''s as easy as that!
int Num = 4 + rand() % 76;
I think it''s as easy as that!
Just in case you want really hardcore random numbers, look into the Mersenne Twister algorithm. It is fast, stable, and very uniform. Note that its still not quite up to par for cryptography, however.
Do NOT do as Raptor85 suggested, doing so will cause the output to be signifigantly less random.
quote:Hey, if you want hardcore ...
Original post by ApochPiQ
Just in case you want really hardcore random numbers, look into the Mersenne Twister algorithm.
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?
quote:
Original post by Beer Hunterquote:Hey, if you want hardcore ...
Original post by ApochPiQ
Just in case you want really hardcore random numbers, look into the Mersenne Twister algorithm.![]()
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
______________________________________________________________
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement