xxrand

package module
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 3, 2022 License: MIT Imports: 1 Imported by: 2

README

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bool

func Bool() bool

Bool returns, as a bool, a pseudo-random number

func Float32

func Float32() float32

Float32 returns, as a float32, a pseudo-random number in [0.0,1.0)

func Float64

func Float64() float64

Float64 returns, as a float64, a pseudo-random number in the half-open interval [0.0,1.0).

func Int31n

func Int31n(n int32) int32

Int31n returns, as an int32, a non-negative pseudo-random number in the half-open interval [0,n) It panics if n <= 0.

func Int32

func Int32() int32

Int32 returns a tread-safe, non-cryptographic pseudorandom int32.

func Int63

func Int63() int64

Int63 returns a non-negative pseudo-random 63-bit integer as an int64

func Int63n

func Int63n(n int64) int64

Int63n returns, as an int64, a non-negative pseudo-random number in the half-open interval [0,n). It panics if n <= 0.

func Intn

func Intn(n int) int

Intn returns, as an int, a non-negative pseudo-random number in the half-open interval [0,n). It panics if n <= 0.

func Uint32

func Uint32() uint32

Uint32 returns a tread-safe, non-cryptographic pseudorandom uint32.

func Uint32n

func Uint32n(maxN uint32) uint32

Uint32n returns a tread-safe, non-cryptographic pseudorandom uint32 in the range [0..maxN).

func Uint64

func Uint64() uint64

Uint64 returns a tread-safe, non-cryptographic pseudorandom uint64.

func Uint64n

func Uint64n(max uint64) uint64

Uint64n returns a tread-safe, non-cryptographic pseudorandom uint64 in the range [0..maxN).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL