Skip to content
Toolssy logo Toolssy

How to Generate Random Numbers in Python

Last updated: July 9, 2026

Python makes random numbers easy with two built-in modules: random for general use and secrets for security-sensitive values. No install needed. Want a no-code option? Use our Random Number Generator.

The random module

Import it once, then use its functions:

import random

A random integer in a range

random.randint(1, 100) returns a whole number from 1 to 100, inclusive.

A random float

random.random() returns a float in [0.0, 1.0). For a range, use random.uniform(1, 10).

Pick from a list

random.choice(['a', 'b', 'c']) returns one random item. random.sample(items, k) returns k unique items (no repeats), and random.shuffle(items) shuffles a list in place.

The secrets module (for security)

For passwords, tokens and anything security-related, use secrets, which is cryptographically strong:

import secrets

secrets.randbelow(100) returns a secure integer from 0 to 99. secrets.token_hex(16) returns a secure random token. Never use the plain random module for passwords or keys.

Reproducible results

Set a seed to reproduce the same sequence, useful for tests and simulations:

random.seed(42)

Quick reference

  • random.randint(a, b) — integer in [a, b]
  • random.uniform(a, b) — float in [a, b]
  • random.choice(seq) — one random element
  • random.sample(seq, k) — k unique elements
  • secrets.randbelow(n) — secure integer in [0, n)

For a fast, visual alternative, try the Random Number Generator or the Random String Generator on Toolssy.