← All notes

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

Claims

Sources

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.

License: CC-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.

Cite this

Permanent ID: iil:note:e2nvm3bz

iinventedlight. 2026. "Python random: pseudo-random numbers, not cryptography." iinventedlight Library, iil:note:e2nvm3bz, version of 2026-11-23. https://iinventedlight.com/library/notes/python-random/. License: CC-BY-4.0.

@misc{iil_note_e2nvm3bz,
  author = {{iinventedlight}},
  title = {{Python random: pseudo-random numbers, not cryptography}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-23},
  url = {https://iinventedlight.com/library/notes/python-random/},
  note = {Permanent ID iil:note:e2nvm3bz. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/python-random.md.