Python Random Number Examples

Python Generate random integers with random from 0 to 10

The simplest generation of a series of random integers is done by from random import randint. Below you can find example of generating 1000 integers in interval from 0 to 10:

from random import randint
import collections

nums = []
for x in range(1000):
    nums.append(randint(0, 10))

print  (collections.Counter(nums))

result:

Counter({7: 112, 9: 101, 4: 100, 2: 93, 5: 93, 1: 92, 8: 88, 10: 88, 0: 85, 3: 74, 6: 74})

In order to verify the randomness we are using group by and count from Collections. AS you can see the result seems to be random. Could we do more random numbers? We could try with from secrets import randbelow

Python 3.6 Generate random integer numbers with secrets - randbelow

If you have Python 3.6 you can use secrets - randbelow to generate numbers. Producing 1000 integers in interval from 0 to 10:

import collections
from secrets import randbelow

nums = []
for x in range(1000):
    nums.append(randbelow(10))

print  (collections.Counter(nums))

result:

Counter({7: 113, 0: 108, 2: 106, 5: 102, 3: 101, 9: 97, 4: 97, 6: 95, 8: 94, 1: 87})

This seems to be better in terms of randomness. And the spread of numbers looks much better. This can be considered as "cryptographically strong" random. You can read more here: PEP 506 -- Adding A Secrets Module

Some common abbreviations used in this proposal:

PRNG:

Pseudo Random Number Generator. A deterministic algorithm used to produce >random-looking numbers with certain desirable statistical properties.

CSPRNG:

Cryptographically Strong Pseudo Random Number Generator. An algorithm >used to produce random-looking numbers which are resistant to prediction.

Python Generate random integer numbers with random choice

You can generate number from a list of values using: random.choice(nums)

import random
import collections

nums = [x for x in range(10)]

rand = []
for x in range(1000):
    rand.append(random.choice(nums))

print (rand)
print  (collections.Counter(rand))

result:

Counter({4: 115, 8: 110, 2: 106, 6: 106, 9: 104, 1: 94, 3: 93, 5: 92, 0: 91, 7: 89})

Python Generate random numbers from predefined list

random.choice(nums) can be used when you want to get random elements of list:

import random
import collections

nums = [2,4,67,3]

rand = []
for x in range(1000):
    rand.append(random.choice(nums))

print (rand)
print  (collections.Counter(rand))

result:

Counter({4: 268, 2: 260, 3: 252, 67: 220})

Python Generate random float numbers with uniform

Below you can find example of generating 1000 floats in interval from 0 to 10:

from random import uniform

nums = []
for x in range(1000):
    nums.append(uniform(0, 10))

result:

0.18487854282652982: 1, 0.6016200481265632: 1, 2.3920220120668825 ...

Python Generate random list from 0 to 20

random.choice(nums) can be used when you want to get random elements of list:

import random
nums = [x for x in range(20)]
random.shuffle(nums)
print(nums)

result:

[6, 7, 1, 2, 17, 0, 10, 3, 5, 15, 14, 11, 19, 8, 18, 12, 4, 16, 9, 13]