DEV Community

Discussion on: Math/Rand in GoLang

leobm profile image
Felix Wittmann • Edited on

You have to set a Seed before call Intn the first time. I think the reason for this is, the random number algorithm is deterministic. For the same Seed you get for a Intn call sequence the same random number Set. I think golang has a default Seed. Because your start random number is every time 81 for 100.

here is an example how you can set the seed by a unix timestamp.



To create "real" random number for a computer is not possible.
Except perhaps when the computer is influenced by other physical processes (user input, e.g. mouse movements to generate a number) All algorithm only give the appearance of randomness. Mostly with the help of time, I think (CPU tick).
For real random numbers you can use a service like this here

They generates randomness via atmospheric noise.

Edit2: I think it's good that the go algorithm works determistic? e.g. when you have to write a test case for code that uses random generation. You can set an own default Seed and can therefore make a prediction of the result.

mquanit profile image
Mohammad Quanit Author

now it makes sense to me. Thank you