DEV Community

Yiğit Erdoğan
Yiğit Erdoğan

Posted on

Harnessing Mathematical Chaos: Building a Python PRNG Using the Collatz Conjecture

Hello DEV Community,

As developers, we frequently rely on standard libraries to handle fundamental computational tasks. When we need a random number, we simply call import random or use the secrets module without delving into the underlying mechanics of entropy, seed generation, or the Mersenne Twister algorithm.

As a third-year Software Engineering student, I wanted to break through that abstraction layer. To truly understand how pseudo-randomness is computationally achieved, I decided to build a Pseudo-Random Number Generator (PRNG) entirely from scratch.

To make the experiment more challenging, I chose to base the entropy engine on one of the most famous unsolved problems in mathematics: the Collatz Conjecture.

I would like to introduce my recent project: BSG Random Number Generator.

The Theory: Why the Collatz Conjecture?

For those who might not be familiar, the Collatz Conjecture (also known as the 3n+1 problem) is a mathematical sequence defined by two simple rules applied to any positive integer:

  • If the number is even, divide it by 2.
  • If the number is odd, multiply it by 3 and add 1.

The conjecture states that no matter what starting value you choose, the sequence will always eventually reach the 4-2-1 loop. However, the path it takes to get there—the "stopping time" and the peak values reached during the sequence—exhibits behavior that is incredibly unpredictable and highly sensitive to the initial state.

This unpredictable orbital path is a perfect example of deterministic chaos. I wanted to investigate whether this chaos could be harvested and mathematically normalized to generate usable, uniformly distributed random numbers.

Architectural Overview and Implementation

The BSG Random Number Generator is written in pure Python with zero external dependencies. The core logic revolves around extracting specific metrics from the Collatz sequence to build an internal state mechanism.

Instead of relying on system time or OS-level entropy pools, the generator uses:

  1. Orbital Lengths: The exact number of steps required for a specific seed to reach the 4-2-1 loop.
  2. Peak Tracking: The maximum integer value achieved during the sequence path.
  3. Parity Sequences: The alternating pattern of odd and even evaluations before termination.

By dynamically updating the seed based on these extracted metrics and applying normalization techniques, the generator successfully outputs floating-point numbers between 0.0 and 1.0, as well as scalable integers. The resulting distribution is surprisingly uniform and demonstrates how mathematical anomalies can be structured into functional code.

Security Limitations and Disclaimer

As a software engineering practice, it is crucial to clearly define the boundaries of experimental projects. This generator is strictly an educational and experimental tool.

While it produces uniform distributions suitable for procedural generation, basic simulations, or non-critical randomized logic, it has not been subjected to rigorous statistical test suites like Diehard or NIST. Furthermore, it is deterministic by nature and is not Cryptographically Secure (CSPRNG). It should never be used for generating cryptographic keys, tokens, or handling sensitive security operations.

Seeking Community Feedback

Building the BSG Random Number Generator was a comprehensive exercise in state management, algorithm optimization, and understanding computational entropy in Python.

I am sharing this project here because I highly value the technical insights of the DEV community. I would appreciate any feedback on the repository, specifically regarding:

  • The structural design and Pythonic efficiency of the code.
  • Potential mathematical strategies to extract even higher levels of entropy from the sequence orbits.
  • Suggestions for optimizing the state transition logic to prevent performance bottlenecks over millions of iterations.

Repository Link: Yigtwxx/bsg-random-number-generator

Have you ever tried rebuilding fundamental standard library tools from scratch to better understand their underlying architecture? I would love to hear your thoughts and experiences in the comments below.

Top comments (0)