github.com/songzhibin97/go-baseutils@v0.0.2-0.20240302024150-487d8ce9c082/sys/fastrand/README.md (about) 1 # fastrand 2 3 Fastest pseudo-random number generator in Go. 4 5 This generator actually come from Go runtime per-M structure, and the init-seed is provided by Go runtime, which means you can't add your own seed, but these methods scales very well on multiple cores. 6 7 The generator passes the SmallCrush suite, part of TestU01 framework: http://simul.iro.umontreal.ca/testu01/tu01.html 8 9 pseudo-random paper: https://www.jstatsoft.org/article/view/v008i14/xorshift.pdf 10 11 fast-modulo-reduction: https://lemire.me/blog/2016/06/27/a-fast-alternative-to-the-modulo-reduction/ 12 13 14 15 ## Compare to math/rand 16 17 - Much faster (~5x faster for single-core; ~200x faster for multiple-cores) 18 - Scales well on multiple cores 19 - **Not** provide a stable value stream (can't inject init-seed) 20 - Fix bugs in math/rand `Float64` and `Float32` (since no need to preserve the value stream) 21 22 23 24 ## Bencmark 25 26 Go version: go1.15.6 linux/amd64 27 28 CPU: AMD 3700x(8C16T), running at 3.6GHz 29 30 OS: ubuntu 18.04 31 32 MEMORY: 16G x 2 (3200MHz) 33 34 35 36 ##### multiple-cores 37 38 ``` 39 name time/op 40 MultipleCore/math/rand-Int31n(5)-16 152ns ± 0% 41 MultipleCore/fast-rand-Int31n(5)-16 0.40ns ± 0% 42 MultipleCore/math/rand-Int63n(5)-16 167ns ± 0% 43 MultipleCore/fast-rand-Int63n(5)-16 3.04ns ± 0% 44 MultipleCore/math/rand-Float32()-16 143ns ± 0% 45 MultipleCore/fast-rand-Float32()-16 0.40ns ± 0% 46 MultipleCore/math/rand-Uint32()-16 133ns ± 0% 47 MultipleCore/fast-rand-Uint32()-16 0.23ns ± 0% 48 MultipleCore/math/rand-Uint64()-16 140ns ± 0% 49 MultipleCore/fast-rand-Uint64()-16 0.56ns ± 0% 50 ``` 51 52 53 54 ##### single-core 55 56 ``` 57 name time/op 58 SingleCore/math/rand-Int31n(5)-16 15.5ns ± 0% 59 SingleCore/fast-rand-Int31n(5)-16 3.91ns ± 0% 60 SingleCore/math/rand-Int63n(5)-16 24.4ns ± 0% 61 SingleCore/fast-rand-Int63n(5)-16 24.4ns ± 0% 62 SingleCore/math/rand-Uint32()-16 10.9ns ± 0% 63 SingleCore/fast-rand-Uint32()-16 2.86ns ± 0% 64 SingleCore/math/rand-Uint64()-16 11.3ns ± 0% 65 SingleCore/fast-rand-Uint64()-16 5.88ns ± 0% 66 ``` 67 68 69 70 ##### compare to other repo 71 72 (multiple-cores) 73 74 rand-1 -> this repo 75 76 rand-2 -> https://github.com/valyala/fastrand 77 78 ``` 79 name time/op 80 VS/fastrand-1-Uint32()-16 0.23ns ± 0% 81 VS/fastrand-2-Uint32()-16 3.04ns ±23% 82 VS/fastrand-1-Uint32n(5)-16 0.23ns ± 1% 83 VS/fastrand-2-Uint32n(5)-16 3.13ns ±32% 84 ``` 85