▶ Random Everything - 100+ Free Generators // v2.5.0
root@generate-random:~/random-number-go$ _

Go Random Number Generator

Random numbers in Go: a random integer in any range, floats, picking and shuffling, and secure random values - with ready-to-paste code

How to generate a random number in Go: math/rand/v2 for everyday use, crypto/rand for anything security-related. Choose a range above for a ready-to-run program.

Random Int in a Range

rand.IntN(n) returns 0 to n-1; add the minimum to shift the range. math/rand/v2 (Go 1.22+) is seeded automatically.

import "math/rand/v2"

n := rand.IntN(10) + 1 // 1..10

Random Float

rand.Float64() returns a value in [0.0, 1.0).

f := rand.Float64()            // 0.0 <= f < 1.0
g := 1.5 + rand.Float64()*3.0  // 1.5 <= g < 4.5

Random Element and Shuffle

Pick by index; rand.Shuffle swaps elements through a callback.

items := []string{"red", "green", "blue"}
pick := items[rand.IntN(len(items))]
rand.Shuffle(len(items), func(i, j int) {
	items[i], items[j] = items[j], items[i]
})

Secure Random Numbers with crypto/rand

math/rand is not suitable for secrets. crypto/rand reads from the operating system CSPRNG; rand.Int returns an unbiased value below a big.Int limit.

import (
	"crypto/rand"
	"math/big"
)

v, err := rand.Int(rand.Reader, big.NewInt(10)) // 0..9
if err != nil {
	panic(err)
}
n := v.Int64() + 1 // 1..10

buf := make([]byte, 16)
_, _ = rand.Read(buf) // 16 random bytes

Common Mistakes

With the original math/rand before Go 1.20, forgetting rand.Seed gave the same sequence on every run; math/rand/v2 removes that trap. Never use math/rand for tokens or keys.

Frequently Asked Questions

How do I generate a random number between 1 and 10 in Go? ▶
rand.IntN(10) + 1 with math/rand/v2. For a secure value use crypto/rand: rand.Int(rand.Reader, big.NewInt(10)), then add 1.
Do I need to seed math/rand in Go? ▶
Not with math/rand/v2 or with math/rand since Go 1.20: both are seeded automatically.
Is math/rand secure? ▶
No. Use crypto/rand for anything security-related.
Is rand.IntN(n) inclusive? ▶
No. It returns 0 to n - 1.

[ HOW TO CITE THIS PAGE ]

APA Style:
Generate-Random.org. (2026). Go Random Number Generator. Retrieved from https://generate-random.org/random-number-go
Web Citation:
Go Random Number Generator - Generate-Random.org (https://generate-random.org/random-number-go)