How to Generate Random Numbers in GoLang

We need random numbers for a lot of reasons. It may be due to some basic reason or cryptographic. Complete random number although is not possible to generate but pseudorandom numbers can be used in their place.

The pseudorandom numbers are a deterministic sequence of numbers that depends on a seed value. In this post, we will see how to generate pseudorandom numbers in the Go programming language.

1. The math/rand package

The rand package contains multiple functions for different types of random values as shown below. Make sure to import the "math/rand" package.

2. Setting the random seed for random number generation

In order to generate random numbers, we need to set the seed. To set the seed of the random number we need to use a function rand.Seed(seed int64). It takes an int64 as input and sets it as a seed.

Now if a constant seed is set, it will output the same numbers. So, we need to make it a variable seed which changes after each call. Using time is a way to do it.

rand.Seed(time.Now().UnixNano())

3. Generating a Random Integer in GoLang

The Intn() function of the rand package can be used to generate an integer in the interval of 0 and n. It takes only one argument, the n or the upper bound. It throws an error if the given argument is less than 0.

v := rand.Int()  // generates a random integer

We can improve it so that we can define lower and upper bounds and the function will generate random within that specified range. Here is how to do that.

v := rand.Intn(max-min) + min    // range is min to max

4. Generating a floating-point random number

Generating a floating-point random number is as easy as generating an int. The float64 function that returns a float between 0.0 and 1.0.

rand.Float64()  

5. Generate an array of random integers

To generate an array of ints, we can create an array. And then add random ints to it. This a handy way of creating an array of ints. Below shown the code to generate an array of ints.

package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {	
	var v [5]int
	rand.Seed(time.Now().UnixNano())
	for i:=0; i<5; i++ {
		v[i] = rand.Intn(100)
	}
	fmt.Println(v) // [0 28 27 62 63]
}

6. The Perm() method of math/rand package

The perm function inside the rand package returns pseudo-random permutation of the integers 0 to n. This is useful when we need to shuffle integers within range.

package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {	
	
	rand.Seed(time.Now().UnixNano())
	
	v := rand.Perm(8)
	
	fmt.Println(v)   // [1 5 3 7 0 2 4 6]
}

7. Generating Cryptographic Random Number in Go

To generate cryptographic random numbers we need to use the "crypto/rand" package. Here are some examples of what can be done with it.

The Intn() function can be used to generate a random integer and the Prime() function can be used to generate a random prime number.

package main

import (
	"fmt"
	"crypto/rand"
)

func main() {	
	p, _ := rand.Prime(rand.Reader, 64)
        fmt.Println(p)  // 18042566733363347837
} 

This is how we can generate random numbers in Go programming language.