Last active
September 11, 2020 17:48
-
-
Save mdouchement/0e0da2642f3f0857912c5f7720b0ef85 to your computer and use it in GitHub Desktop.
Lehmer random bytes slice generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
) | |
func main() { | |
seed := uint64(time.Now().UnixNano()) | |
p := make([]byte, 128) | |
for i := 0; i < 2; i++ { | |
seed = LehmerRand(seed, p) | |
fmt.Println(p) | |
} | |
} | |
// LehmerRand generates pseudo random bytes. | |
// See https://en.wikipedia.org/wiki/Lehmer_random_number_generator | |
func LehmerRand(seed uint64, p []byte) uint64 { | |
for i := 0; i < len(p); i++ { | |
// Like `seed = seed * 48271 % 2147483647` but 64% speedup | |
seed *= 48271 | |
seed = (seed & 0x7fffffff) + (seed >> 31) | |
p[i] = byte(seed) | |
} | |
return seed | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment