Writing a 36 bit random number generator
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
A 36-bit random number generator means you want outputs in the range 0 through 2^36 - 1. The first design question is whether you need cryptographic randomness or just a deterministic pseudo-random generator for simulation, testing, or games. Those are different problems, and the implementation should reflect that difference immediately.
If you need real unpredictability, do not invent your own PRNG
For security-sensitive use, the answer is simple: ask a cryptographically secure source for 36 random bits.
That gives you a value in the correct 36-bit range without asking you to design your own randomness algorithm.
If you need a deterministic PRNG, generate a wider state and mask
For simulations or reproducible testing, a common pattern is to use a well-behaved generator with a larger internal state and then keep only the low 36 bits of each output.
This gives deterministic 36-bit outputs while keeping a larger internal state than 36 bits.
Why output size and state size are different concerns
Many people hear "36-bit generator" and assume the internal state must also be 36 bits. That is not necessary. The output width can be 36 bits even if the generator internally uses 64 bits, 128 bits, or more.
In practice, using a larger internal state is often the better choice because:
- periods are usually longer
- quality is often better
- implementation can still produce exactly 36-bit values by masking
What matters is that the returned value fits the desired range.
A simple LCG can work, but quality is limited
If you want a very small educational generator, an LCG can produce 36-bit outputs too.
This satisfies the 36-bit requirement, but statistically it is usually weaker than better modern PRNG designs.
Think about period and bias
A generator that outputs the correct numeric range is not automatically a good generator. Important properties include:
- period length
- distribution quality
- correlation between successive values
- suitability for the problem domain
For testing or simulation, use a generator with known behavior. For security, use a cryptographic source. Do not judge a generator only by whether the numbers "look random."
Validate the 36-bit range explicitly
Whatever implementation you use, verify that every value stays in the required range.
That sounds obvious, but range mistakes happen quickly when shifts, masks, and signed integer behavior are mixed carelessly.
Common Pitfalls
- Writing a custom PRNG for security-sensitive use instead of using a cryptographic source.
- Confusing 36-bit output width with 36-bit internal state requirements.
- Assuming a simple LCG is good enough for every simulation problem.
- Forgetting to mask or bound the output so it really stays in the 36-bit range.
- Evaluating random quality only by visual inspection of a few numbers.
Summary
- A 36-bit RNG means outputs must lie in
0through2^36 - 1. - For security, use a cryptographic source such as
secrets.randbits(36). - For deterministic pseudo-random output, use a reasonable PRNG and mask to 36 bits.
- Output width and internal state size are different design choices.
- Choose the generator based on the job, not just on whether it can emit 36-bit values.
Related reading
- Writing an algorithm to decide whether a target number can be reached with a set of other numbers and specific operators?
- Writing your own square root function
- XGBoost/ XGBRanker to produce probabilities instead of ranking scores
- XOR Operation Intuition
- Yolo v1 bounding boxes during training step
- Your favourite algorithm and the lesson it taught you
- YouTube URL algorithm?
- Zig-zag scan an N x N array

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.