Generating a range of random integers

Started by
22 comments, last by dtmf2 22 years, 1 month ago
How could i generate a range of random integers from say 10 -100?
this is my computer. There are many like it, but this one is mine.
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
-- Eric
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
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
heh...great minds, etc.
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.
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...)

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
-- Eric
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 ]
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

This topic is closed to new replies.

Advertisement