Skip to content

Instantly share code, notes, and snippets.

@layou233
Created November 22, 2025 15:39
Show Gist options
  • Select an option

  • Save layou233/0cc35c2aa90343c978f71462fea0818a to your computer and use it in GitHub Desktop.

Select an option

Save layou233/0cc35c2aa90343c978f71462fea0818a to your computer and use it in GitHub Desktop.
Faster source for math/rand/v2 based on runtime rand
// License: CC0
package frs
import (
"hash/maphash"
"unsafe"
)
// RuntimeSource is a non-reproducible, cryptographically insecure math/rand/v2 pseudorandom source.
// Based on maphash.MakeSeed that exports runtime rand, with the output based on the internal per-G state of the runtime,
// which is unknown to userspace, shared per-G and therefore cannot be exported for reproduction.
//
// WARNING: Do not use this for cryptographical usages. Use crypto/rand instead.
type RuntimeSource struct{}
func (s RuntimeSource) Uint64() uint64 {
type seed struct {
s uint64
}
r := maphash.MakeSeed()
return (*seed)(unsafe.Pointer(&r)).s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment