Random Numbers

The Random Module

To make anything random in python, you need a module called Random. This module was to be imported to your script beforehand like this:

import random

Think of this as a spellbook! Every python spell must derive from the spellbook. In this case, that spellbook we need is random.

A Basic Number Generator

Lets analyze the following generator:

import random
# Generate a random integer between 1 and 10 (inclusive)
random_integer = random.randint(1, 10)
print(f"Random Integer: {random_integer}")
Random Integer: 2

import random: Allows us to access the random module

random_integer: This is out variable to hold the number we generate

random.randit(1,10): This basically says “from the random module, use the randint function and use parameters 1 and 10.” Randint is the function used to determine a random integer in a range. In this case that eange is 1 and 10.

print(f"Random Integer: {random_integer}"): Prints the number

Another type of generator

Lets look at one more generator:

import random
# Generate a random float between 0 and 1 (inclusive)
random_float = random.uniform(0, 1)
print(f"Random Float: {random_float}")
Random Float: 0.1742626429831371

Before we jump in, lets clarify what the different types of random are. Lets compare Randint, Uniform, and normalvariate to find out:

Randint:

  • What it does: Picks a whole number between two values, with equal chance for each.
  • Distribution type: Discrete uniform distribution.

Uniform:

  • What it does: Picks a decimal (float) between two numbers, again with equal chance for every value in that range.
  • Distribution type: Continuous uniform distribution.

Normalvariate:

  • What it does: Picks a number where values near the average (mean) are more likely, and extreme values are rare.
  • Distribution type: Normal (bell curve).

This code right here uses Uniform. It needs to pick a float or decimal and the Uniform function works best when dealing with decimals between 2 numbers with equal chance to roll it.

Hacks!

Here are a few challenges you can try making with the concept of RNG in Python:

  1. Dice or Lottery Numbers
  2. Random Decimal Precision
  3. Random Shuffle with Numbers
Learn About Random Module & RNG

Learn the Random Module & Try RNGs

About Python’s random Module

The random module in Python is used to generate random numbers. Some key functions include:

  • random(): Returns a float between 0.0 and 1.0.
  • randint(a, b): Returns an integer between a and b (inclusive).
  • uniform(a, b): Returns a float between a and b.
  • choice(seq): Returns a random element from a sequence.

Generate Random Float (0.0 – 1.0)

Generate Random Integer (randint(a, b))


Generate Random Float in Range (uniform(a, b))


Extra Interactive Demos (Python)

Below are small, self-contained Python snippets you can run inside the notebook. They use the standard random module and a tiny seeded helper so results can be reproduced for tests.

# Seeded RNG helper using random.Random (reproducible)
from random import Random, choices, randint, shuffle, uniform, gauss

def make_rng(seed):
    return Random(seed)

rng = make_rng(42)
print('rng sample floats:', [rng.random() for _ in range(3)])

# Two-dice simulation and heatmap (counts for face1 vs face2)
import numpy as np
import matplotlib.pyplot as plt
from random import Random

def two_dice_heatmap(trials=10000, seed=1):
    rng = Random(seed)
    grid = np.zeros((6,6), dtype=int)
    for _ in range(trials):
        a = rng.randint(1,6) - 1
        b = rng.randint(1,6) - 1
        grid[a,b] += 1
    return grid

g = two_dice_heatmap(5000, seed=7)
print(g)
# If matplotlib is available, show a simple heatmap (optional)
try:
    plt.imshow(g, cmap='viridis')
    plt.colorbar()
    plt.title('Two-dice frequency heatmap (face1 x face2)')
    plt.xlabel('Die 2 face (1-6)')
    plt.ylabel('Die 1 face (1-6)')
    plt.show()
except Exception as e:
    print('matplotlib display skipped:', e)

# Loot box simulator (with adjustable probabilities)
from random import Random

def simulate_loot(n=1000, seed=0, pool=None):
    if pool is None:
        pool = [('Common',0.7), ('Rare',0.25), ('Epic',0.045), ('Legendary',0.005)]
    rng = Random(seed)
    names = [p[0] for p in pool]
    weights = [p[1] for p in pool]
    counts = {name:0 for name in names}
    for _ in range(n):
        pick = rng.random()
        s = 0
        for name,w in zip(names, weights):
            s += w
            if pick < s: counts[name] += 1; break
    return counts

print(simulate_loot(1000, seed=42))

# Seeded shuffle and reproducible playlist example
from random import Random

def seeded_shuffle(arr, seed):
    rng = Random(seed)
    a = list(arr)
    rng.shuffle(a)
    return a

songs = ['Song A','Song B','Song C','Song D','Song E']
print('seed=1:', seeded_shuffle(songs,1))
print('seed=1 again:', seeded_shuffle(songs,1))
print('seed=2:', seeded_shuffle(songs,2))