# Sample code for random Numbers in GoLang

• 2020-06-12 09:30:41
• OfStack

Random Numbers, we all know, is a computer through some algorithm, "random" to generate a number. Many programming languages have built-in methods for generating random Numbers, but what about GoLang?

Pseudo random number

We all know how "random Numbers" work in real life. You can flip a coin and say it's random, but in a computer there's a standard way to determine if a "random number" is really a "random number."

According to the principles of cryptography, there are the following criteria for randomness test of a "random number" :

Statistical pseudo-randomness - in a given random bitstream sample, the number of ones is approximately equal to the number of zeros, that is, the number of "10", "01", "00" and "11" are approximately equal. In human terms, "1 seems random". Cryptography security pseudo-randomness - that is, given a random sample of part 1 and random algorithm, can not effectively calculate the rest of the random sample. True randomness - defined as a random sample that cannot be reproduced.

According to the above criteria, the corresponding random Numbers can also be divided into the following categories:

Pseudorandom Numbers - random Numbers that satisfy condition 1. Cryptographically secure pseudo-random Numbers - random Numbers that satisfy both of the first two conditions. It can be calculated by cryptography secure pseudorandom number generator. True random number - a random number that meets all three criteria.

Knowing the above concepts, we know that "pseudo-random number" is actually a "seemingly random, but not really random" number.

Pseudo-random number generator

Pseudorandom Numbers are sufficient in most practical applications. These sequences are "seemingly" random Numbers, but they are actually generated by a fixed, repeatable calculation. Because they're actually computable, they're not really random, but they have statistical properties similar to random Numbers. The generator that produces this result is called a pseudo-random number generator.

Only in cryptography scenarios do we need to use true random Numbers.

In most programming languages, "pseudo-random number generators" are provided, such as Math.random () in JS and math/rand packages in GoLang.

Pseudo-random Numbers in GoLang

In GoLang, we can generate a pseudo-random number through the method in math/rand package:

``````
package main
import (
"fmt"
"math/rand"
)
func main() {
fmt.Println(rand.Int())  // => 134020434
}``````

In the above code, we generate a pseudo-random number using the rand.Int () method. It doesn't seem to be a problem. I'm very 39en.

But if you're careful, you'll find yourself running the same code on your own computer as I do. It doesn't matter how you run it.

We know that Math.random () in JS returns a different number each time, but the pseudo-random number generator in GoLang returns the same number by default.

They're all pseudorandom number generators. Why are they so different? So here's the idea of a random seed.

A random seed

We know that pseudorandom Numbers are Numbers that are calculated using a deterministic algorithm that appears to be in a random order, so pseudorandom Numbers are not actually random.

So, of course, the order of pseudo-random Numbers calculated by the algorithm is the same if the starting value is the same.

This "starting value" is called a random seed.

Looking through the documentation, we learn that the Int() function is a pseudo-random number generated from default Source (the default source).

And this default Source, as we saw in the Seed section, if you don't set the random seed, the default initial seed always starts at 1.

If you have one random seed, then you have one random seed.

Random pseudo-random Numbers

We already know that the default random seed starts at 1, so if we just set a different seed before each random number is generated, the result will not be the same.

As much as possible, we want to make sure that the PSEUDO-random number generator works with a different seed each time, usually using the current time as the seed.

``````
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(int64(time.Now().UnixNano()))
fmt.Println(rand.Int())
}

``````

Thus, because of the different seeds, we won't get the same result each time we run. So we can get pseudo random Numbers.

True random number

If our application has high security requirements and needs to use true random Numbers, we can use the method in the crypto/rand package.

``````
package main
import (
"crypto/rand"
"fmt"
"math/big"
)
func main() {
//  generate  20  a  [0, 100)  True random number of the range.
for i := 0; i < 20; i++ {