"Package
cryptotestprovides deterministic randomness for testing."
That's the opening line of the Go authors' documentation for testing/cryptotest, introduced in Go 1.27. Anytime your code touches crypto/rand, each test run pulls fresh system entropy — meaning a test that fails in CI passes cleanly on your laptop. So how do you reproduce and debug a failure like that?
Why tests using crypto/rand resisted debugging
The obvious idea is to swap the randomness source for your own io.Reader feeding predetermined bytes. But in modern versions of crypto/rsa and crypto/ecdsa, the standard library increasingly insists on real system entropy and flat-out ignores custom readers.
Even with a stub in your own code, crypto algorithms still go straight to the OS for randomness. A rare edge-case bug in key or token generation turns into a ghost you can't catch by rerunning the test.
How SetGlobalRandom locks and resets the entropy stream
In Go 1.27.3, SetGlobalRandom from testing/cryptotest solves this:
func SetGlobalRandom(t *testing.T, seed uint64)
Passing a seed switches the process's global entropy source into deterministic mode — affecting both direct crypto/rand.Read calls and hidden entropy draws inside crypto/... packages. The t *testing.T parameter scopes the override to the test's lifetime: once the test finishes, the runtime restores the system generator automatically.
A natural question follows: what if you need to run generation twice inside the same test and compare outputs? Calling SetGlobalRandom again with the same seed resets the stream back to its starting state.
I spotted a working random_test.go example from the Go Developer Library Telegram channel, verified on Go 1.27.3. The test pins seed 42, reads 16 bytes — a typical size for a password-reset token or JWT check — then resets the stream and reads 16 bytes again:
package mycrypto_test
import (
"bytes"
"crypto/rand"
"testing"
"testing/cryptotest"
)
func TestDeterministicEntropyStream(t *testing.T) {
cryptotest.SetGlobalRandom(t, 42)
firstChunk := make([]byte, 16)
if _, err := rand.Read(firstChunk); err != nil {
t.Fatalf("first rand.Read failed: %v", err)
}
cryptotest.SetGlobalRandom(t, 42)
secondChunk := make([]byte, 16)
if _, err := rand.Read(secondChunk); err != nil {
t.Fatalf("second rand.Read failed: %v", err)
}
if !bytes.Equal(firstChunk, secondChunk) {
t.Fatal("entropy stream did not reset deterministically")
}
t.Logf("deterministic_entropy_verified: len=%d bytes_match=%v", len(firstChunk), bytes.Equal(firstChunk, secondChunk))
}
Notice there's no hardcoded byte array anywhere in the test. The first SetGlobalRandom(t, 42) sets the starting point for firstChunk, the second resets the stream before filling secondChunk, and bytes.Equal verifies they match byte-for-byte.
To bypass Go's test cache and run the scenario fresh, use:
go test -v -count=1 ./...
-count=1 disables caching, and the test log confirms both 16-byte reads match bit-for-bit. Capturing a single seed is now enough to reproduce a key or token generation failure locally without rewriting your code around mocks.
Global substitution does come with two strict limits:
- No
t.Parallel():SetGlobalRandomswaps the generator for the entire OS process, so neither the test nor any of its parent tests can run in parallel — otherwise the test panics. - FIPS-140 mode: when built with Go Cryptographic Module v1.0.0 (where
crypto/fips140.Versionreturns"v1.0.0"), the function is unsupported.
Determinism in crypto/rand holds right up to the boundary of parallelism and certified crypto modules.
If you want a broader look at reliability tooling in the language, check out our Go 1.27 release overview and our breakdown of fuzz testing in Go.
And the most important caveat is best left in the testing/cryptotest docs' own words: how crypto algorithms consume randomness isn't frozen in the standard and can change over time — so a test asserting a specific cryptographic output may still break in a future Go release, even with SetGlobalRandom.

