1664525 and 1013904223 are the classic “Numerical Recipes” parameters for a 32-bit linear congruential generator
Xₙ₊₁ = (1664525 · Xₙ + 1013904223) (mod 2³²)
m = 2³² = 4 294 967 296, not float.MaxValue.
What your code really does
(1664525 * input + 1013904223) % float.MaxValue / float.MaxValue
1664525 * input + 1013904223 is evaluated with 32-bit int arithmetic.
For input = 10 000 the value is 17 655 425 223, well below 2³¹-1.
- Because the right-hand operand of
% is a float (float.MaxValue) the
left side is first converted to a float (≈ 1.7655426 × 10¹⁰).
float.MaxValue is ≈ 3.4 × 10³⁸, so
value % float.MaxValue == value – nothing is reduced.
- You now divide that ≈ 10¹⁰ by ≈ 10³⁸, ending up with ≈ 10⁻²⁸ – hence the
“1.4E-30”.
So the small number is exactly what you asked for:
your generator’s result divided by an astronomically larger number.
How to implement the LCG correctly
Use an unsigned 32-bit integer as the state, let it overflow naturally
(mod 2³²), and then scale the result to the range [0, 1).
public sealed class Lcg32
{
private uint _state; // Current state
private const uint A = 1664525u; // Multiplier
private const uint C = 1013904223u; // Increment
public Lcg32(uint seed) => _state = seed;
// 32-bit unsigned integer output
public uint NextUInt()
{
_state = A * _state + C; // overflows => mod 2^32
return _state;
}
// Floating-point value in [0,1)
public float NextFloat()
{
// 1 / 2^32, but use double first for better precision
const double k = 1.0 / 4294967296.0; // 2^32
return (float)(NextUInt() * k);
}
}
Usage
var rng = new Lcg32(12345); // any non-zero seed
Debug.Log(rng.NextFloat()); // 0.0 ≤ x < 1.0
If you want an int or double variant, simply change the return type or
scaling factor.
Key take-aways
• float.MaxValue (≈ 3.4 × 10³⁸) is not the LCG modulus – use 2³².
• Do the recurrence in an unsigned 32-bit integer; let overflow do the % 2³² for you.
• To map the integer to [0, 1) divide by 4 294 967 296 (= 2³²), not by float.MaxValue.