Python Random – Generate Random Numbers with Specific Digits

pythonrandom

How do I generate a random number from a specific set of digits? For example,
I want to generate numbers from the range 1-100,000 such that every number has only odd digits (for example: 111, 1351, 19711 etc..)

Using the random module I tried:

import random

rand = random.randint([1, 3, 5, 7, 9]) 

Is there any efficient way of doing it?
Thank you.

Best Answer

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})