Golang random bool and random boolean: math/rand and crypto/rand

Golang random bool and golang random boolean with math/rand Intn and Float64, golang rand local source for tests, random boolean generator patterns in Go, and crypto/rand when you need unpredictable bits—not Python or Java.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Golang random bool and random boolean: math/rand and crypto/rand

If you need a golang random bool or golang random boolean for games, simulations, or tests, math/rand is the usual choice: rand.Intn(2) == 1 gives a fair coin flip, and rand.Float64() < 0.5 is an equivalent pattern. The golang rand package is pseudo-random and fast; for a random boolean generator where unpredictability matters (tokens, lottery, security), use crypto/rand instead. This page is Go-only—queries such as random boolean python or java random boolean map to other ecosystems. For language basics, see Getting started with Golang.

Tested with Go 1.24 on Linux.


Golang random bool with math/rand

rand.Intn returns a uniform integer in [0, n). For a boolean, use n == 2:

go
package main

import (
	"fmt"
	"math/rand"
)

func main() {
	for i := 1; i <= 10; i++ {
		fmt.Println(i, rand.Intn(2) == 1)
	}
}
Output

Since Go 1.20, the default global source is automatically seeded; you should not call rand.Seed on every iteration (it is deprecated for the global generator and can hurt quality if misused).

Float64 or Float32 threshold

Another common pattern:

go
rand.Float64() < 0.5

Use the same precision (Float64 vs Float32) consistently if you mix with other rand calls.

Local rand.Rand for reproducible streams

When you need a deterministic stream (unit tests, simulations), create your own generator with a fixed seed:

go
package main

import (
	"fmt"
	"math/rand"
)

func main() {
	r := rand.New(rand.NewSource(42))
	for i := 0; i < 5; i++ {
		fmt.Println(r.Intn(2) == 1)
	}
}
Output

A *rand.Rand is not safe to share across goroutines without your own synchronization; use one Rand per goroutine or protect with a mutex.


Unpredictable booleans: crypto/rand

For security-sensitive booleans, read a byte and test the low bit (handle error from Read):

go
package main

import (
	"crypto/rand"
	"fmt"
)

func cryptoBool() (bool, error) {
	var b [1]byte
	if _, err := rand.Read(b[:]); err != nil {
		return false, err
	}
	return b[0]&1 == 1, nil
}

func main() {
	v, err := cryptoBool()
	if err != nil {
		panic(err)
	}
	fmt.Println(v)
}

Mark as {run=false} if your environment hides Run for crypto/rand imports—run with go run locally.


Summary

Golang random bool generation for simulations and games is idiomatically rand.Intn(2) == 1 or rand.Float64() < 0.5 using math/rand; rely on the auto-seeded global generator in current Go versions instead of re-seeding in tight loops. For a golang random boolean that must resist guessing, build a small helper on crypto/rand. Use a dedicated rand.New source when you need repeatable sequences or isolated goroutine streams.


References


Frequently Asked Questions

1. How do I get a golang random bool?

Use rand.Intn(2) == 1 or rand.Float64() < 0.5 with the top-level math/rand functions; since Go 1.20 the global generator is auto-seeded—avoid calling rand.Seed in a loop.

2. What is the difference between math/rand and crypto/rand for a random boolean?

math/rand is fast and deterministic if you seed it yourself for tests; crypto/rand reads unpredictable bytes suitable for security but is slower and needs error handling.

3. Is rand.Seed still recommended?

rand.Seed is deprecated for the global source; create your own rand.New(rand.NewSource(seed)) when you need a reproducible stream in tests or simulations.

4. Does this page cover Python or Java random boolean?

No—searches like random boolean python or java random boolean belong to those languages; here all examples are Go.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …