---
source: https://iinventedlight.com/library/notes/python-random.md
title: "Python random: pseudo-random numbers, not cryptography"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "PSF License Version 2 (Python Software Foundation); PSF License Version 2 (CPython); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, random — Generate pseudo-random numbers; CPython, random module documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-23
status: published
approved_by: "Cole Withington, 2026-11-27 14:30 PT: \"Yes, publish all of that now\""
checked_on: 2026-11-23
as_of: 2026-11-23
confidence: medium
untested: "Not reproduced (2026-11-23). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: python-random"
tags: [python, random, stdlib]
client: Librarian
sources:
  - id: S1
    title: "random — Generate pseudo-random numbers"
    publisher: Python Software Foundation
    url: https://docs.python.org/3/library/random.html
    accessed: 2026-11-23
    license: "PSF License Version 2"
  - id: S2
    title: "random module documentation (source)"
    publisher: CPython
    url: https://raw.githubusercontent.com/python/cpython/main/Doc/library/random.rst
    accessed: 2026-11-23
    license: "PSF License Version 2"
---
# Python random: pseudo-random numbers, not cryptography
**Answer:** The basic random() function returns a float uniformly from the half-open range 0.0 <= X < 1.0. The core generator is the Mersenne Twister. The output is deterministic and completely unsuitable for cryptographic purposes.

## The core generator

Almost all module functions depend on that basic random() function.

Python uses the Mersenne Twister as the core generator, producing 53-bit precision floats.

It has a period of 2**19937-1. The C implementation is fast and threadsafe.

Because it is completely deterministic, it is not suitable for all purposes, and it is completely unsuitable for cryptographic purposes.

## Module API

For sequences, there is uniform selection of a random element. Another function generates a random permutation of a list in-place. A third function does random sampling without replacement.

The supplied functions are bound methods of a hidden Random instance.

Instantiate your own Random instances if you want generators that don't share state.

## See also

- [python-http-client](https://iinventedlight.com/library/notes/python-http-client/)
- [python-smtplib](https://iinventedlight.com/library/notes/python-smtplib/)
- [python-itertools](https://iinventedlight.com/library/notes/python-itertools/)

## Claims

- id: C1
  claim: random() generates a float uniformly in the half-open range 0.0 <= X < 1.0.
  source: S1
  evidence: "random float uniformly in the half-open range"

- id: C2
  claim: Python uses the Mersenne Twister as the core generator.
  source: S1
  evidence: "Python uses the Mersenne Twister as the core generator"

- id: C3
  claim: The generator is completely unsuitable for cryptographic purposes.
  source: S1
  evidence: "completely unsuitable for cryptographic purposes"

- id: C4
  claim: The module functions are bound methods of a hidden Random instance.
  source: S2
  evidence: "bound methods of a hidden instance of the"

## Sources

- [S1] random — Generate pseudo-random numbers. Python Software Foundation. https://docs.python.org/3/library/random.html . License: PSF License Version 2. Accessed 2026-11-23.

- [S2] random module documentation (source). CPython. https://raw.githubusercontent.com/python/cpython/main/Doc/library/random.rst . License: PSF License Version 2. Accessed 2026-11-23.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Python Software Foundation, random — Generate pseudo-random numbers; CPython, random module documentation (source). PSF-2.0 (https://docs.python.org/3/license.html). Copyright Python Software Foundation. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
