Random number generator (RNG) generates a set of values that do not display any distinguishable
patterns in their appearance. The random number generators are divided into two categories:
hardware random-number generators and pseudo-random number generators. Hardware random-number
generators are believed to produce genuine random numbers. Pseudo-random number generators
generate values based on software algorithms. They produce values that look random. But these
values are deterministic and can be reproduced, if the algorithm is known.
In computing, random generators are used in gambling, gaming, simulations, or cryptography.
To increase the quality of the pseudo random-number generators, operating systems use
environmental noise collected from device drivers, user input latency, or jitter from one or
more hardware components. This is the core of the cryptographically secure pseudo-random
number generators.
For security purposes, cryptographically secure pseudo-random number generators must be used.
This function returns a random float number between 0.0 to 1.0. The function doesn’t
need any arguments.
import random
print(random.random())
randint(a, b)
This function returns a random integer between the two integers a
and b
inclusive.
import random
print(random.randint(1, 10))
The uniform(a, b)
function returns a random float number between two given parameters
a
(inclusive) and b
(exclusive).
import random
print(random.uniform(1, 10))
The randrange(start, stop, step)
function returns a randomly selected element from the
range created by the start, stop, and step arguments. The stop parameter is required while
the others are optional.
import random
print(random.randrange(0, 101, 10))
creating a list of random vals
import random
rvals = [random.randrange(10, 110, 10) for _ in range(10)]
print(rvals)
The choice(seq)
function returns a random element from the non-empty sequence seq.
import random
print(random.choice('abcdefghij'))
We can specify population sample k
and probability weights.
import random
res = random.choices('abcdef', k=3, weights=[1, 5, 3, 1, 1, 10])
print(res)
The shuffle(seq)
function shuffles the sequence seq in place.
import random
list = [20, 16, 10, 5]
random.shuffle(list)
print(list)
The sample(population, k)
function returns a k
length new list of elements chosen
from the population sequence or set.
import random
print(random.sample(range(10, 30), 3))
The example creates a bag of colored balls (represented by colors) with different
quantities (counts). It then uses random.sample
to pick 4 balls, considering the
number of each color available (using the counts attribute). The output will likely
have more 'red' balls than others since there are more red ones in the bag.
import random
# Simulate a bag of balls (colors) with different counts
colors = ['red', 'green', 'blue']
counts = [3, 2, 1] # Red has more balls
# Sample 4 elements, respecting the counts
chosen_colors = random.sample(colors, 4, counts=counts)
print(chosen_colors) # Output: Likely includes more 'red' than others