Skip to content

Fast, scalable pseudo random number generator based on xxh3

License

Notifications You must be signed in to change notification settings

kelindar/xxrand

Repository files navigation

kelindar/xxrand
Go Version PkgGoDev Go Report Card License Coverage

XXH3-Based Pseudorandom Number Generator

This package contains an experimental implementation of a noise based pseudorandom number generator that scales with multiple CPUs and from my benchmarks around 10x faster than using math.Rand. It uses xx3 algorithm to hash a counter, the default counter being the time stamp counter (using RDTSC instruction). That being said, most of use-cases probably are better of with using math.Rand since it should provide better randomness characteristics.

Features

  • Optimized to scale on multiple CPUs when called without the state (e.g. Int31n()).
  • Supports most of math/rand functions.
  • Roughly 10x faster than math/rand equivalent.

What is this for?

  • You can use this in the benchmarks where where math/rand tends to create a lock contention, especially when you are benchmarking multiple goroutines in parallel and generating some random data as you go.
  • You can use this in building games with Go, where performance is critical and potentially you are generating a lot of random numbers.
  • You can use this for use-cases that do not require extremely random numbers, such as load-balancing for example.

Benchmark Results

cpu: Intel(R) Core(TM) i7-9700K CPU @ 3.60GHz
BenchmarkParallel/rand-001-8            394602625                3.023 ns/op           0 B/op          0 allocs/op
BenchmarkParallel/rand-008-8            360640208                3.130 ns/op           0 B/op          0 allocs/op
BenchmarkParallel/rand-032-8            368496836                3.492 ns/op           0 B/op          0 allocs/op
BenchmarkParallel/rand-128-8            392360864                3.041 ns/op           0 B/op          0 allocs/op
BenchmarkParallel/rand-512-8            388266206                3.043 ns/op           0 B/op          0 allocs/op
BenchmarkParallel/rand-2048-8           382660137                3.097 ns/op           0 B/op          0 allocs/op

About

Fast, scalable pseudo random number generator based on xxh3

Resources

License

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published