-1

So I was using the random module in Python with some loops and was printing out a batch of numbers to check what they looked like. I noticed that when I input:

random.randint(0,100000)

most of the numbers would be six figure numbers with a few at five figures and fewer at 4. There were barely any single figures at all. It makes me question how random rand.int really is.

user2925800
  • 105
  • 3
  • 11

2 Answers2

5

Between 0 and 100000, 90% of the numbers have 5 figures! Only 0.01% have 1 figure. So the behavior is what I'd expect. EDIT: And note what ignacio says. The numbers are definitely not "truly" random as that would require some sort of quantum event. They are "pseudo" random numbers.

ooga
  • 15,423
  • 2
  • 20
  • 21
3

To expand on ooga's answer, between 0 and 100,000, there are:

  • 10 single-digit numbers (0-9)
  • 90 two-digit numbers (10-99)
  • 900 three-digit numbers (100-999)
  • 9000 four-digit numbers (1,000-9,999)
  • 90000 five-digit numbers (10,000-99,999)

This is why you'd expect a 90% chance of a random number having five digits, a 9% chance of four digits, a .9% chance of three digits, and so on. Another way to look at it is to think of any positive number with fewer than five digits as having a 0 in the ten thousands place (e.g. 07,734), so there are nine times as many five-digit numbers, since there are nine non-zero possibilities for the ten thousands place.

(Also, I'm assuming you meant you're getting mostly five-figure numbers or the range went up to 1,000,000, since there shouldn't be any six-figure integers between 0 and 100,000.)

Milo P
  • 1,377
  • 2
  • 31
  • 40