You're encountering extremely small numbers because of how floating-point arithmetic interacts with large constants and float.MaxValue in your Linear Congruential Generator (LCG) expression:
(1664525 * input + 1013904223) % float.MaxValue / float.MaxValue;
Let’s break down why this returns something like 1.408492E-30.
🔍 Problem Analysis
1. float.MaxValue is huge
float.MaxValue ≈ 3.402823e+38
Your constants:
1664525 * input + 1013904223 — even for input = 10000, this is:
1664525 * 10000 = 16,645,250,000
+ 1,013,904,223 = ~17.66e9
So result ≈ 1.766e10, which is much smaller than float.MaxValue (~3.4e38).
Thus:
(1.766e10) % float.MaxValue → just 1.766e10 (since it's less than float.MaxValue)
Then you do:
1.766e10 / float.MaxValue ≈ 1.766e10 / 3.4028e38 ≈ 5.19e-29
Which is on the order of 1e-29, matching what you're seeing (1.4e-30 may vary slightly based on input).
So the output is not an error — it's mathematically correct, but not useful for typical pseudo-random number generation.
✅ Why This Is a Problem
You're trying to implement an LCG and normalize to [0, 1) by dividing by float.MaxValue, but:
- You're using integer-style LCG constants (like those for 32-bit unsigned integers), but applying them in a floating-point modulus context.
float is not precise enough for large integer arithmetic — it has only ~7 decimal digits of precision, so numbers like 1e9 already lose precision.
% float.MaxValue doesn't make sense here — modulus with a float isn't standard for LCGs.
✅ Correct Approach: Use Integer Arithmetic
LCGs are defined over integers. You should use an integer type (like uint or int) for the state, then convert to float at the end.
✅ Proper LCG Implementation (Normalized to [0,1))
float LCG(int seed)
{
uint state = (uint)seed;
uint a = 1664525;
uint c = 1013904223;
uint next = (a * state + c);
// Convert to float in [0, 1)
return (float)next / uint.MaxValue;
}
Note: uint.MaxValue = 4,294,967,295 ≈ 4.3e9
Now:
next ≈ 1.766e10 → but wait! This overflows uint.MaxValue!
Ah! Another issue: 1664525 * 10000 + 1013904223 = ~17.66e9 > uint.MaxValue (~4.29e9)
So you must use modular arithmetic:
uint next = (a * state + c) % uint.MaxValue;
Or better, use the standard full-period LCG modulus: 2^32 (which uint naturally wraps due to overflow — but C# doesn't auto-wrap signed types).
✅ Safe Version with Overflow Handling
float LCG(uint seed)
{
uint a = 1664525;
uint c = 1013904223;
uint next = a * seed + c; // Automatically wraps modulo 2^32
return (float)next / uint.MaxValue;
}
Because uint arithmetic in C# wraps around on overflow (if not in checked context), this gives you the correct LCG behavior.
✅ Example
Debug.Log(LCG(10000)); // Now returns something like 0.768...
This will give you a well-distributed float in [0, 1).
🚫 Why Not Use float.MaxValue?
- It's not a power of two, and not used in LCGs.
- It's way too large (~3e38), so dividing by it makes everything tiny.
- LCGs rely on modulus like
2^32 or 2^31 - 1, not floating-point limits.
✅ Summary: Fix
Replace this:
(1664525 * input + 1013904223) % float.MaxValue / float.MaxValue;
With this:
(uint)(1664525 * input + 1013904223) % uint.MaxValue;
return (float)result / uint.MaxValue;
Or better, use uint throughout:
float LCG(uint input)
{
uint next = 1664525u * input + 1013904223u;
return (float)next / uint.MaxValue;
}
This will give you sensible values in [0, 1).
Let me know if you want a seeded random float generator class!