Go math/rand: pseudo-random numbers for simulation, not secrets
Answer: Package math/rand produces pseudo-random numbers that are fine for simulation but must not be used for security-sensitive work.
Sources, Rand, and concurrency
Numbers come from a Source, usually wrapped in a Rand.
Each of those types should be used by a single goroutine at a time; sharing them across goroutines needs extra synchronization.
Top-level functions such as Float64 and Int are safe for concurrent use by several goroutines.
This package's outputs might be easily predictable, no matter how the generator is seeded.
For random numbers that must be suitable for security-sensitive work, see crypto/rand.
The top-level Read helper, unlike Rand.Read, is safe for concurrent use, always reports len(p), and returns a nil error, but it is deprecated in favor of crypto/rand.Read for almost all cases.
Seeding and compatibility
Seed values that leave the same remainder when divided by 2³¹-1 generate the same pseudo-random sequence.
The top-level Seed function, unlike Rand.Seed, is safe for concurrent use.
If you never call Seed, the generator is seeded randomly at program startup.
Before Go 1.20 it started as if Seed(1) had been called; you can restore that by calling Seed(1) at startup or by setting GODEBUG=randautoseed=0 before any call into the package.
As of Go 1.20, there is no remaining reason to call Seed just to inject a random value.
If you need a specific, repeatable sequence, use New(NewSource(seed)) to get a local generator.
As of Go 1.24, Seed is a no-op.
NewSource returns a new Source seeded with the given int64.
A Source itself is not safe for concurrent use by multiple goroutines.
Shuffle panics if n < 0.
See also
Claims
- id: C1 claim: This generator is not appropriate for security-sensitive work. source: S1 evidence: "it should not be used for security-sensitive work."
- id: C2 claim: Package-level Read is safe for concurrent callers. source: S1 evidence: "Read, unlike the Rand.Read method, is safe for concurrent use."
- id: C3 claim: Shuffle panics when n is negative. source: S2 evidence: "Shuffle panics if n < 0."
Sources
- [S1] rand package - math/rand. The Go Authors. https://pkg.go.dev/math/rand . License: BSD-3-Clause. Accessed 2026-11-26.
- [S2] rand.go (source). The Go Authors. https://go.dev/src/math/rand/rand.go . License: BSD-3-Clause. Accessed 2026-11-26.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: The Go Authors, rand package - math/rand; The Go Authors, rand.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: The Go Authors, rand package - math/rand; The Go Authors, rand.go (source). BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.
Cite this
iinventedlight. 2026. "Go math/rand: pseudo-random numbers for simulation, not secrets." iinventedlight Library, iil:note:tcw6ykv0, version of 2026-11-26. https://iinventedlight.com/library/notes/go-math-rand/. License: CC-BY-4.0.
@misc{iil_note_tcw6ykv0,
author = {{iinventedlight}},
title = {{Go math/rand: pseudo-random numbers for simulation, not secrets}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-26},
url = {https://iinventedlight.com/library/notes/go-math-rand/},
note = {Permanent ID iil:note:tcw6ykv0. License: CC-BY-4.0.},
}
Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).
Extra files beside this note
These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.
- go-math-rand.schema.json (schema; CC-BY-4.0)
- go-math-rand.table.json (table; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-math-rand.md.