Skip to main content

🛡️ FortRand: A Fortified Mersenne Twister for Python

Version

FortRand is a high-performance, statistically-hardened Random Number Generator (RNG) implemented as a C-extension for Python. It provides the legendary speed of the Mersenne Twister (MT19937) while mitigating its inherent mathematical predictability through periodic entropy injection.


🚀 The Core Concept: Hybrid Randomness

Standard PRNGs (like Python's built-in random) follow a purely deterministic mathematical path once seeded. While excellent for simulations, they can be "solved" if enough output is observed.

FortRand achieves a "Best of Both Worlds" architecture by pairing the high-speed MT19937 algorithm with a security-focused modification where the internal "twist" function is "hardened" to periodically inhale fresh entropy from the OS kernel. This hybrid approach allows the generator to pass the most rigorous statistical batteries while maintaining a throughput significantly higher than raw system entropy.


🔬 Technical Architecture: XOR State Mutation

The core of FortRand's security lies in its periodic state mutation. To break the linearity of the Mersenne Twister, FortRand performs a Full-State XOR with high-quality OS entropy during the twist operation By XORing the internal 624-word state with raw entropy, the generator effectively "moves the goalposts" for any potential state-reconstruction attack, making previously observed outputs useless for predicting future values.

📊 Performance & Validation

FortRand isn't just "conceptually" better; it has been mathematically proven through standardized testing.

Efficiency (rands/second)

  • Python random.Random: ~2.53e+06 r/s (Fast, but mathematically predictable)
  • Python random.SystemRandom : ~0.54e+05 r/s (Secure, but slow system calls)
  • FortRand: ~2.88e+06 r/s (Fastest + Hardened)

NIST SP 800-22 Certification

FortRand successfully passed the full NIST Statistical Test Suite. Its Linear Complexity p-values are significantly more uniform than the standard Mersenne Twister, confirming that the entropy injection successfully breaks linear patterns.

Dieharder Battery

Achieved a 100% PASSED assessment across all tests, including those where the standard library occasionally shows "WEAK" results.


🛠️ Installation

Prerequisites

  • Python 3.11+

Install

pip install fortrand

Basic Usage

import fortrand

# Works just like the standard random module!
x = fortrand.randint(1, 100)
print(x)

⚖️ Attribution & Credits

FortRand is a derivative work of the CPython Random module source code.

  • The core MT19937 algorithm was developed by Takuji Nishimura and Makoto Matsumoto.
  • The Python integration logic was originally wrapped by Raymond Hettinger and the PSF.

This project is released under the MIT License, but contains sub-components governed by the PSF License and the original 3-clause BSD Mersenne Twister license.

Release files for fortrand 1.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fortrand 1.0.0
File Size Uploaded
fortrand-1.0.0.tar.gz 24.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fortrand 1.0.0
File Interpreter ABI Platform
fortrand-1.0.0-cp311-cp311-win_amd64.whl CPython 3.11 CPython 3.11 Windows x86-64 Details

Total release size: 54.2 kB

Release files / fortrand-1.0.0.tar.gz

Download URL fortrand-1.0.0.tar.gz
Size 24.4 kB
Tags Source
SHA-256 checksum
How to use checksums
2c7cd44979b7d040a82c70bf4984426a80900827073a62dc1c7584a76ba30744
BLAKE2b-256 checksum
How to use checksums
451c401664faa3ac836534f9e6b67140025b1aed2ee80192d9df21a1a8bb5df4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.4

Release files / fortrand-1.0.0-cp311-cp311-win_amd64.whl

Download URL fortrand-1.0.0-cp311-cp311-win_amd64.whl
Size 29.8 kB
Tags CPython 3.11 Windows x86-64
SHA-256 checksum
How to use checksums
ae57633e8d0566e1c582ce4e8e03d2170c5c2255bf0ba375467360bae60a65eb
BLAKE2b-256 checksum
How to use checksums
b613ba903345faf46e34aed4631048653b199aebe784a09490bb10763d077f84
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.4

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page