Go math/rand algorithm?

(蔡翼成) #1

Does anyone know which algorithm is used by Go math/rand? I looked at source code and googled it, but I got no clue. It said only uniform distribution from plan 9 project in Bell lab. The link, https://github.com/seehuhn/mt19937, said that the result or math/rand is the same as Mersenne Twister.
Could anyone provide the reference of the algorithm behind math/rand?

Thank you in advance.

(Clifford Bernard) #2

The algorithm is found in the link below:


Also, the issue with Rand is that if you use the same seed, the results will always come out the same. Below is an example of providing a Rand of a Rand using time. The GoPlay.Space does not offer time correctly; however, if you put that into your IDE, it works fine. I hope this helps. Good luck.

Rand of a Rand Example:

(Norbert Melzer) #3

Why is this an issue?

I consider this as expected, and about every PRNG algorithm works like this…

(Clifford Bernard) #4

I agree, but in practice with people learning to code in general, this can be a confusing on the face of it. New coders expect Random to be Random, LOL.

(蔡翼成) #5

Thank you for the reply.
I also find some interesting posts.
A nice graphic illustration post: https://appliedgo.net/random/
& rngSource https://golang.org/src/math/rand/rng.go
But I still no idea about the mathematical theory behind it (numbers generated by uniform distribution).

(Yusuke Saito) #6

Lagged Fibonacci Generator

“If I am not mistaken again, the generator is an ALFG (Additive Lagged Fibonacci Generator, thats what Wikipedia calls it). Knuth describes the algorithm in Volume 2 of The art of computer programming in section 3.2.2 (around equation 7). Both Wikipedia and Knuth state the parameter combination 607,273 as possible combination with a period length of 2^(e-1) * (2^607-1) where e is the length of the random number in bits.
I actually found a few references examining its properties and it seems to be a good rng so faar, but there is still seems to be a lack of mathematical background and it is fairly easy to get into trouble by not seeding properly.”


(蔡翼成) #7

Thank you very much Saito San. It’s very helpful.
I checked the List of RNG
There is no comment on LFG.
I would like to know the comparison of LFG & Mersenne Twister.
Because, I plan to use Mersenne Twister (MT) or better.
Does anyone know if MT is better than LFG?


Math/rand and crypto/rand differences
(system) closed #8

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.