Skip to main content

Fortified Mersenne Twister for Python

Project description

🛡️ 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fortrand-1.0.0.tar.gz (24.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fortrand-1.0.0-cp311-cp311-win_amd64.whl (29.8 kB view details)

Uploaded CPython 3.11Windows x86-64

File details

Details for the file fortrand-1.0.0.tar.gz.

File metadata

  • Download URL: fortrand-1.0.0.tar.gz
  • Upload date:
  • Size: 24.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for fortrand-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2c7cd44979b7d040a82c70bf4984426a80900827073a62dc1c7584a76ba30744
MD5 44b9c50cfc7c8c473ca0b458d3ea3e01
BLAKE2b-256 451c401664faa3ac836534f9e6b67140025b1aed2ee80192d9df21a1a8bb5df4

See more details on using hashes here.

File details

Details for the file fortrand-1.0.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: fortrand-1.0.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 29.8 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for fortrand-1.0.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 ae57633e8d0566e1c582ce4e8e03d2170c5c2255bf0ba375467360bae60a65eb
MD5 2302e0ba10ccb5e73b5f02ef6eccb7e9
BLAKE2b-256 b613ba903345faf46e34aed4631048653b199aebe784a09490bb10763d077f84

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page