← All notes

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

Sources

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

Permanent ID: iil:note:tcw6ykv0

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.},
}

Markdown

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.

All artifact files (JSON index)

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.