Cookies management by TermsFeed Cookie Consent

πŸ”’ Generate a random number in Go

Last updated:
introduction random

One common task in programming is generating random numbers, which can be useful for a variety of applications such as simulations, games, cryptography, and more.

To generate a random number in Go (actually, it’s pseudo-random, but in this article, we will refer to it as random), you just need to use the Go built-in math/rand package that provides functions for generating random numbers.

In the examples below, we will show you how to generate any random number, as well as how to create functions to generate random int and float64 numbers in specific ranges.

In Go, the math/rand package provides functions to generate pseudo-random numbers. They are generated using an algorithm that produces a sequence of numbers that appear to be random, but are actually deterministic. The sequence of numbers produced by the algorithm can be replicated if the initial state or seed is known.

Pseudo-random numbers are sufficient for most applications where randomness is required, such as games and simulations. However, in applications where security is a concern, such as cryptography, true random numbers should be used to ensure the highest level of randomness and unpredictability. In Go, the crypto/rand package provides functions to generate true random numbers.

Generate any random integer number

To generate any integer random number in Go, just use the Int() function from the math/rand package. This function generates a non-negative random integer over the [0, MaxInt] range.

As of Go 1.20, there is no need to set the seed before calling functions that generate random numbers, and the Seed() function is marked as deprecated.

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    fmt.Printf("random integer: %d\n", rand.Int())
}

An example output:

random integer: 5696350091330895579

You can run this application a couple of times to see that you get different results each time.

The math/rand package contains many methods to generate different types of random numbers, for example:

See the package documentation for more information.

Generate a random float64 number

In the same way as an integer, you can also generate a random floating-point number. The only thing you need to be aware of is that the Float64() function (and other floating-point functions) generate a number in the range [0.0,1.0).

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    fmt.Printf("random float64: %f\n", rand.Float64())
}

Output:

random float64: 0.764513

Generate a random int number over a range

In many use cases, however, you need to generate a number in a given range, such as an integer in the range [5,10), instead of a completely arbitrary integer. There is no function in the math/rand package that does this automatically, but we can use the Intn(n int) function to achieve a similar result. This function returns a random number in the range [0, n), so to get a value in the range [min, max) we can generate a value from [0, max-min) using this function, and add min to the result.

Look at the example to see exactly how it is done.

package main

import (
    "fmt"
    "math/rand"
)

func randInt(min, max int) int {
    return min + rand.Intn(max-min)
}

func main() {
    fmt.Printf("random integer in range [5, 10): %d\n", randInt(5, 10))
}

Output:

random integer in range [5, 10): 6

Generate a random float64 number in a given range

In a similar way as for integers, you can generate a random floating-point number from the range [min, max). In the following example, we create the randFloat() function for this purpose. It uses the built-in function rand.Float64() to generate a number in the range [0, 1). This number is then multiplied times (max-min) and shifted by the value of min to get a number in the range [min, max).

package main

import (
    "fmt"
    "math/rand"
)

func randFloat(min, max float64) float64 {
    return min + rand.Float64()*(max-min)
}

func main() {
    fmt.Printf("random float64 in range [2.4, 3.2): %f\n", randFloat(2.4, 3.2))
}

Output:

random float64 in range [2.4, 3.2): 3.142109

Thank you for being on our site 😊. If you like our tutorials and examples, please consider supporting us with a cup of coffee and we'll turn it into more great Go examples.

Have a great day!

Buy Me A Coffee

🎲 Generate a random string in Go

Learn how to generate a random string of a fixed length
introduction random strings

♾️ Infinite loop in Go

Learn how to define a “while true” loop
introduction loop

πŸ“” Convert a struct to io.Reader in Go

Learn how to convert a struct to io.Reader and send it as an HTTP POST request body
introduction http