3 ms·
C++ has had a few options since C++11[1]. - Linear congruential generator[2]: xₜ = a xₜ₋₁ + c mod m - Mersenne Twister[3]: too complicated for a few lin
by GrantMoyer 2y ago
C++ has had a few options since C++11[1].
- Linear congruential generator[2]:
xₜ = a xₜ₋₁ + c mod m
- Mersenne Twister[3]:
too complicated for a few lines
- Subtract with carry[3]:
k < p
carryₜ = [xₜ₋ₖ - xₜ₋ₚ - carryₜ₋₁ < 0]
xₜ = xₜ₋ₖ - xₜ₋ₚ - carryₜ₋₁ mod 2ⁿ
All three algorithms are fairly popular.
(Please excuse the unidiomatic index and constant names, I'm at the mercy of unicode)
[1]: https://en.cppreference.com/w/cpp/header/random https://en.cppreference.com/w/cpp/header/random
[2]: https://en.wikipedia.org/wiki/Linear_congruential_generator https://en.wikipedia.org/wiki/Linear_congruential_generator
[3]: https://en.wikipedia.org/wiki/Mersenne_Twister https://en.wikipedia.org/wiki/Mersenne_Twister
[4]: https://en.wikipedia.org/wiki/Subtract_with_carry https://en.wikipedia.org/wiki/Subtract_with_carry
- adgjlsfhk1 2y agoNote that all 3 of these are fairly bad. Xoshiro and PCG are the 2 modern families of good fast rngs.