Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

dtmf2

Generating a range of random integers

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
For that specific range, use the CRT rand() function like so:


(rand() % 90) + 10


That gives you 10-99, actually; modify as you see fit. For general ranges you could use a function similar to this:


int RandInRange( int min, int max )
{
// TODO: Assertions or checks for min < max, both > 0, etc.
return (rand() % (max - min)) + min;
}



--
Eric

Edited by - ekenslow on February 18, 2002 12:01:15 AM

Share this post


Link to post
Share on other sites
Eric's method reduces the randomness of the number returned. You'd probably only notice it if getting lots of random numbers. To get around that problem, do this:

any_type Value = ((rand()/(float)RAND_MAX)*(max - min))+min;


Edit: Left something out



Edited by - Null and Void on February 18, 2002 12:06:32 AM

Share this post


Link to post
Share on other sites
I''m tipsy from this wonderful Chimay Grande Reserve I just finished off, so I''ll point out (again) that using % with rand() won''t give you a nice distribution of random numbers - you should use the upper bits instead.

But of course, if your random numbers don''t matter too much, % is a perfectly fine way to go.

-scott

Share this post


Link to post
Share on other sites
I''m tipsy from this wonderful Chimay Grande Reserve I just finished off

You appear to have excellent taste. May I recommend Duvel and Kwak.

Share this post


Link to post
Share on other sites
You most certainly may recommend those - they are both great. As is Orval, Hapkin, Brigand, Piraat....oh the list is so long.

My favorite pasttime of late have been lambics, especially those from the Cantillon brewery. Last year, I was blessed to taste their apricot lambic on handpump in a local beer bar (where I will be headed tonight, as a matter of fact!). Pure enjoyment. (so sour, yet so good...)

Share this post


Link to post
Share on other sites
My assumption was that speed and simplicity were more critical than pure randomness. If randomness is critical, the CRT rand() function should be avoided anyway. See this thread for a discussion of alternate RNGs. Simply scaling the values returned from rand() is not sufficient, as rand() is just not that random to begin with. You''d be throwing away the advantages of speed and simplicity for no real gain.

In any case, I''m skeptical that using modulo on rand() reduces its randomness, although I''m not capable of proving it either way. Does anybody have a link to a good source about this?

--
Eric

Share this post


Link to post
Share on other sites
Firstly, random number generators are just patterns. A modulus operation is essentially another kind of pattern, and the two may coincide to produce a 3rd pattern. One example is that on some systems, random number generators tend to generate an odd number, then an even one, and so on. So rand() % 2 is often too predictable.

Secondly, unless the range is a multiple of the number you''re dividing by to get the remainder, then your result is biased a tiny amount towards the lower numbers. This is usually an insignificant amount, but that depends on how big your range is. If the max range of rand() was 32767 and you wanted a number between 1 and 20000, then rand() % 20000 is going to give you twice as much chance of scoring 10000 as it would of scoring 15000.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
We dont use modulus because as many have said it alters the distribution. Instead you scale. If you need 0 to 20000 and you are getting 0 to 32767 then you multiply by 20000/32767 thus maintaining the distribution.

this works well:

rand()*(high-low+1)/RAND_MAX)+low

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!