What i would love to do is to create a function that takes a parameter that is the limit of which number the random generation should create. I have experienced that some generators that just repeat the number generated over and over again.
How can I make a generator that doesn't return the same number consecutively. Can someone please help me to achieve my goal?
int randomGen(int max)
{
int n;
return n;
}
The simplest way to get uniformly distributed results from
rand
is something like this:The result will be in the range
0
tolimit-1
, and each will occur with equal probability as long as the values0
throughRAND_MAX
all had equal probability with the originalrand
function.Other methods such as modular arithmetic or dividing without the loop I used introduce bias. Methods that go through floating point intermediates do not avoid this problem. Getting good random floating point numbers from
rand
is at least as difficult. Using my function for integers (or an improvement of it) is a good place to start if you want random floats.Edit: Here's an explanation of what I mean by bias. Suppose
RAND_MAX
is 7 andlimit
is 5. Suppose (if this is a goodrand
function) that the outputs 0, 1, 2, ..., 7 are all equally likely. Takingrand()%5
would map 0, 1, 2, 3, and 4 to themselves, but map 5, 6, and 7 to 0, 1, and 2. This means the values 0, 1, and 2 are twice as likely to pop up as the values 3 and 4. A similar phenomenon happens if you try to rescale and divide, for instance usingrand()*(double)limit/(RAND_MAX+1)
Here, 0 and 1 map to 0, 2 and 3 map to 1, 4 maps to 2, 5 and 6 map to 3, and 7 maps to 4.These effects are somewhat mitigated by the magnitude of
RAND_MAX
, but they can come back iflimit
is large. By the way, as others have said, with linear congruence PRNGs (the typical implementation ofrand
), the low bits tend to behave very badly, so using modular arithmetic whenlimit
is a power of 2 may avoid the bias problem I described (sincelimit
usually dividesRAND_MAX+1
evenly in this case), but you run into a different problem in its place.Any pseudo-random generator will repeat the values over and over again with some period. C only has
rand()
, if you use that you should definitively initialize the random seed withsrand()
. But probably your platform has better than that.On POSIX systems there is a whole family of functions that you should find under the
man drand48
page. They have a well defined period and quality. You probably find what you need, there.How about this:
Without explicit knowledge of the random generator of your platform, do not do
rand() % max
. The low-order bytes of simple random number generators are usually not random at all.Use instead (returns a number between min inclusive and max non-inclusive):
Update: The solution above is biased (see comments for explanation), and will likely not produce uniform results. I do not delete it since it is a non natural example of what not to do. Please use rejection methods as recommended elsewhere in this thread.