One way could be to define a list of odds from which to sample, but keeping in mind the how likely it should be for a number to be sampled randomly. Since there are ten times as many 2 digit numbers than 1 digit numbers, we need to set the weights of these sampling sizes according to this logic.
Following this reasoning, we could use numpy.random.choice
, which allows for sampling from a list following a probability distribution:
from numpy.random import choice
odds = ['1','3','5','7','9']
n_digits = 5 # up to 99999 for ex
range_digits = list(range(1,n_digits))
weights = [5**i for i in range_digits]
weights_sum = sum(weights)
probs = [i/weights_sum for i in weights]
sizes = choice(range_digits,size=n,p=probs)
[int(''.join(choice(odds,size))) for size in sizes]
# [3151, 3333, 1117, 7577, 1955, 1793, 5713, 1595, 5195, 935]
Let's check the generated distribution for 10_000
samples:
from collections import Counter
sizes = choice(range_digits,size=10_000,p=probs)
out = [int(''.join(choice(odds,size))) for size in sizes]
Counter(len(str(i)) for i in out)
# Counter({4: 8099, 3: 1534, 2: 304, 1: 63})
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…