mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-07-26 06:38:42 +00:00
add runtime.fastrandn (#5502)
This commit is contained in:
@@ -15,6 +15,14 @@ func fastrand() uint32 {
|
||||
return xorshift32State
|
||||
}
|
||||
|
||||
// fastrandn is the equivalent of fastrand()%n, but faster. Some packages
|
||||
// (e.g. wireguard-go's device/timers.go) reach it via
|
||||
// //go:linkname ... runtime.fastrandn, so it must exist in the runtime.
|
||||
func fastrandn(n uint32) uint32 {
|
||||
// See https://lemire.me/blog/2016/06/30/fast-random-shuffling/
|
||||
return uint32(uint64(fastrand()) * uint64(n) >> 32)
|
||||
}
|
||||
|
||||
func initRand() {
|
||||
r, _ := hardwareRand()
|
||||
xorshift64State = uint64(r | 1) // protect against 0
|
||||
|
||||
Reference in New Issue
Block a user