1

I hope this question is sufficiently question-worthy and that I haven't missed the point. I understand that there are likely multiple answers - I will mark the best one I get correct, but if this is not an OK question to ask then please say and I will delete as appropriate.

If I am using python scripts where there's lots of (more than one) random numbers required, e.g.

from random import randrange

number1 = randrange(10)
number2 = randrange(10)
number3 = randrange(10)

print number1, number2, number3

...then is randrange the best way to do it? Specifically, how random actually is it? I feel like I notice that it has a sort of... bias?

Like, repeatedly seems to get the same values.

I might be imagining it.

But obviously I know computers don't do random well (at all), and I was wondering how this module is even seeded or whatever...

Is there a better way to generate my random numbers? Like a module that's "more random" or a way to give it a "more random" seed?

5
  • 5
    Repeatedly getting the same values is inherent to randomness. If you never got the same values, that would be bias. If you roll a six sided die a thousand times and never get two sixes in a row, it's rigged. Commented Aug 17, 2017 at 12:53
  • Chekc this info, is for use of random and maybe be helpful to you: docs.python.org/3/library/random.html# Commented Aug 17, 2017 at 12:55
  • You should look into generating numbers and then performing a chi square test on them. Commented Aug 17, 2017 at 12:55
  • 1
    you have several random generators, normally random, uniformilly random, poisson random, depending on the distribution you want. For a simple implementation such as yours, you don't have to worry, the pseudorandom generator should be fine, also, the seed number will generate you the same results over and over, which is perfect when you want to share your work and show the same results. Commented Aug 17, 2017 at 12:56
  • 1
    "I feel like I notice that it has a sort of... bias?" Don't feel. Test! Humans are very good at perceiving patterns when they don't actually exist. Commented Aug 17, 2017 at 13:52

3 Answers 3

2

Better to use SystemRandom for better unpredictability. randrange is a pseudorandom number generator which uses Mersenne Twister.

from random import SystemRandom

srand = SystemRandom()

num1 = srand.choice(range(10))
num2 = srand.choice(range(10))
num3 = srand.choice(range(10))

print num1, num2, num3
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! All good comments and I see that what I am already doing is almost certainly fine, but since your answer offers an alternative with more unpredictability I'll go with that!
1

This is heavily dependent on what degree of randomness you're looking for. Python provides os.urandom with the intent of cryptographic use; but that reads from /dev/urandom, not /dev/random, so it might not be random enough for some extreme cases (discussion on unix & linux stack exchange). The secrets module was developed for a clearer choice, while the random module is clearly pseudo-random. That pseudo-randomness is usually enough, provided the seed is not repeated (doing that is helpful for things like repeatable tests or regenerating identical procedurally generated games, which is why we have getstate and setstate).

In general, our ability to find patterns in randomness far exceeds our ability to recognize randomness.

1 Comment

Thanks for your comment - and especially the last one!
1

I don't know much about the differences of random or pseudo-random but if you run the same randrange command for 10 million times you can see that the percentages of repeats are very close. So I don't think you can spot any non-random behavior just by printing the generated numbers and looking at them (not in this case at least).

from random import randrange
number_of_repeats = [0,0,0,0,0,0,0,0,0,0]
percentage = {}
loop_count = 10000000
for _ in range(loop_count):
    number_of_repeats[randrange(10)]+=1
for num,i in enumerate(number_of_repeats):
    percentage[num] = (i/loop_count)*100
print(percentage)

1 Comment

Nicely illustrated!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.