简体   繁体   中英

Python randint generates numbers with the same number of digits

for i in range(number_of_numbers):
 randomNumber = randint(1, pow(10, numberPower))
 numbers.append(randomNumber)
 numbersIncreasing.append(randomNumber)

So I'm making a little math game that generates n amount of numbers from user input. This is the code that generates the numbers and appends them to a list. The only problem is, is it gives numbers with the same amount of digits, which is whatever numberPower is set to (12). An example would be two numbers like 9,564,713,666,123 or 1,256,286,233,753, and all the numbers have 12 digits. I was expecting more very mixed numbers, that could literally be anywhere between 1 and 10^12.

This is because most numbers between 1 and 10**12 have 12 digits. Specifically, 90% of numbers between 1 and 10^12 have 12 digits. Then 9% have 11 digits; only 1% have 10 digits or less.

  • 9 numbers have 1 digit (1-9);
  • 90 numbers have 2 digits (10-99);
  • 900 numbers have 3 digits (100-999);
  • 9000 numbers have 4 digits (1000-9999);
  • ...
  • 9 * 10**10 numbers have 11 digits (10000000000-99999999999);
  • 9 * 10**11 numbers have 12 digits (100000000000-999999999999);
  • 1 number has 13 digits (1000000000000).

If you want, you can try to draw a histogram showing how many numbers have how many digits, when randomly generating a large number of numbers between 1 and 10**12:

import random
import matplotlib.pyplot as plt
X = [random.randint(1,10**12) for _ in range(10000)]
nb_digits = [len(str(x)) for x in X]
plt.hist(nb_digits, bins=range(1,14), density=True)
plt.show()

Another way to think about it: leading zeroes

Generating a number uniformly at random between 0 and 10**12 - 1 is the same as generating 12 digits uniformly at random between 0 and 9, and concatenating them to write a number in base 10.

The "number of digits" of the resulting number will be 12 minus the number of leading zeroes .

For instance, the string 000000049608 counts as a 5-digit number, because the first 7 digits are zeroes.

Thus, if the leftmost digit generated is not a 0, then the resulting number will count as a 12-digit number. This happens whenever the first digit is 1,2,3,4,5,6,7,8 or 9, which is 90% of the time.

What is the probability to get a number with 5 digits or less, like above? It's the probability that the leftmost 7 digits are all zeroes. The probability that this happens is (1/10)**7 = 0.00001%. As you can see, most numbers don't have a lot of leading zeroes.

If you want to skew the distribution to get more diversity in number of digits

Maybe you don't want your random numbers to follow a uniform distribution? Try this:

import random

number_of_numbers = 100
numberPower = 12

numbers = []
for _ in range(100):
  randomNumberOfDigits = random.randint(1, numberPower)
  randomNumber = random.randint(10**(randomNumberOfDigits - 1), 10**randomNumberOfDigits - 1)
  numbers.append(randomNumber)

print(numbers)
# [4, 42, 6004, 2, 46111, 2023179648, 41991462, 525, 2412, 221700769, 47, 5304427, 102, 23, 54323975, 906, 894363079057, 556, 675354, 9365725, 838, 490947621, 82648961, 9232330, 2885693, 28889965, 31601, 505250081, 534874, 85, 4630, 5, 8088582, 67921960, 722774315903, 16, 33, 7752943, 16628713055, 74, 2561429212, 67626556043, 155962, 596, 8655352595, 4735, 7, 8296258, 17722, 78, 93044214350, 17247469, 58295330, 622, 332, 450232321717, 70292459, 11, 447064711120, 895793, 8, 540110, 43032518210, 7, 20361, 606, 9, 9943927, 983, 54327, 40473362991, 2358, 7102932592, 15960846, 99385316379, 88654316391, 219692155, 3732404, 5474, 6343805291, 74, 844532493412, 2, 45228, 186785825, 10704, 4833, 744164008354, 976046952, 7836, 49344611762, 34985277, 362, 49577, 868571104, 237506, 2, 920019513002, 83847, 77908803]

Note: I chose to write ** for power instead of ^ , because we're illustrating with python code and in python, ** means power while ^ means something entirely different.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM