Search code examples
c++c++11mathrandomuniform-distribution

Is it possible for a uniform discrete distribution (uniform_int_distribution) to generate sequential (or the same) numbers when used correctly?


Going to put my edit up here:

In the comments, the actual nature of randomness has been discussed. What I do understand is that no number is more or less likely than another to be generated when generating "random" numbers.

Hopefully a fairer re-phrase of my question is:

If no number is more or less likely than another, then, why can a uniform distribution function call itself that if the numbers aren't always going to be uniformly distributed?


Just to be clear before I ask, I am only asking out of curiosity. Lucky for me I am not a cat.

I have created a quick sort function for a University project, and thought that maybe (rather than just bashing away at the keyboard for a while) I could test it with some lists of integers generated randomly.

std::vector<int> unsorted_list;
std::random_device ran_dev; // Initialise a new random device
std::uniform_int_distribution<> uni_dist(1, 100); // Create Uniform Distribution
for (int i = 0; i < 5; i++) {
    unsorted_list.push_back(uni_dist(ran_dev));
}

Can I fully rely on this implementation to produce random numbers that do always appear to actually be random (evenly distributed)?

You might think I'm being silly... Clearly the class states that it is a uniform distribution, as in, it's not possible to be non-uniform... But are any of these outputs even possible? And if so, how likely?

(1, 1, 1, 1, 1)
(1, 2, 3, 4, 5)
(2, 4, 6, 8, 10)

I suppose the last output is more likely than the first or second (especially when using bounds of 0 to 100), but my curiosity still has me.
Am I being particularly foolish? I've never been very good at maths, so, although I'm beginning to understand symbols and terminology, I still look at some explanations and simply "glaze over", so my googling so far hasn't been helpful.


Solution

  • You are much more likely to get a list with lots of different numbers than a list with lots of repeats. However, a true RNG will produce any number of 1s in a row if used long enough, and inasmuch as a PRNG is supposed to look like a true RNG, a PRNG might be expected to do the same.

    True RNGs typically have independent trials, meaning the outcome of one trial does not tell you anything about the outcome of the other. Knowing one hundred 1s have come up in a row tells you nothing about what such a RNG will do on the next trial. Inasmuch as PRNGs are meant to look like RNGs, you might expect the same from them.

    In summary, it is possible, though unlikely, to get a lot of repeats. If you want no repeats, I would recommend picking elements yourself and then shuffling. That way the order is random whereas the elements have whatever distribution you want, exactly.