fastrand64-go: github.com/Villenny/fastrand64-go Index | Files

package fastrand64

import "github.com/Villenny/fastrand64-go"

Package fastrand64 implements fast pesudorandom number generator that should scale well on multi-CPU systems.

Use crypto/rand instead of this package for generating cryptographically secure random numbers.

Example:

import "github.com/villenny/fastrand64-go"

make a threadsafe random generator
rng := NewSyncPoolXoshiro256ssRNG()

// somewhere later, in some goproc, one of lots, like a web request handler for example
// this (ab)uses a sync.Pool to allocate one generator per thread
r1 := rng.Uint32n(10)
r2 := rng.Uint64()
someBytes := rng.Bytes(8)

// This will produce R1=<random int 0-9>, R2=<random unsigned 64bit int>, someBytes=<random bytes>
fmt.Printf("R1=%v, R2=%v, someBytes=%v", r1, r2, someBytes)

Index

Package Files

fastrand64.go

func Bytes Uses

func Bytes(r UnsafeRNG, bytes []byte) []byte

Bytes fills a []byte array with random bytes from a thread unsafe RNG

func NewUnsafeRandRNG Uses

func NewUnsafeRandRNG(seed int64) *rand.Rand

NewUnsafeRandRNG creates a new Thread unsafe PRNG generator using the native golang 64bit RNG generator (thus avoiding using any global state)

func Splitmix64 Uses

func Splitmix64(index uint64) uint64

Splitmix64 is typically used to convert a potentially zero seed, into better non-zero seeds ie seeding a stronger RNG

type ThreadsafePoolRNG Uses

type ThreadsafePoolRNG struct {
    // contains filtered or unexported fields
}

ThreadsafePoolRNG core type for the pool backed threadsafe RNG

func NewSyncPoolRNG Uses

func NewSyncPoolRNG(fn func() UnsafeRNG) *ThreadsafePoolRNG

NewSyncPoolRNG Wraps a sync.Pool around a thread unsafe RNG, thus making it efficiently thread safe

func NewSyncPoolXoshiro256ssRNG Uses

func NewSyncPoolXoshiro256ssRNG() *ThreadsafePoolRNG

NewSyncPoolXoshiro256ssRNG conveniently allocations a thread safe pooled back xoshiro256** generator this uses NewSyncPoolRNG internally

func (*ThreadsafePoolRNG) Bytes Uses

func (s *ThreadsafePoolRNG) Bytes(n int) []byte

Bytes allocates a []byte filled with random bytes and returns it. This is convenient but caller does the allocation pattern is better way since it can reduce allocation count/GC

func (*ThreadsafePoolRNG) Int63 Uses

func (s *ThreadsafePoolRNG) Int63() int64

Int63 is here to match Source64 interface, why not call Int64

func (*ThreadsafePoolRNG) Read Uses

func (s *ThreadsafePoolRNG) Read(p []byte) []byte

Read fills a []byte array with random bytes from a thread safe pool backed RNG

func (*ThreadsafePoolRNG) Seed Uses

func (s *ThreadsafePoolRNG) Seed(seed int64)

Seed is only here to match the golang std libs Source64 interface

func (*ThreadsafePoolRNG) Uint32n Uses

func (s *ThreadsafePoolRNG) Uint32n(maxN int) uint32

Uint32n returns pseudorandom Uint32n in the range [0..maxN).

It is safe calling this function from concurrent goroutines.

func (*ThreadsafePoolRNG) Uint64 Uses

func (s *ThreadsafePoolRNG) Uint64() uint64

Uint64 returns pseudorandom uint64. Threadsafe

type UnsafeRNG Uses

type UnsafeRNG interface {
    Uint64() uint64
}

UnsafeRNG is the interface for an unsafe RNG used by the Pool RNG as a source of randomness

type UnsafeXoshiro256ssRNG Uses

type UnsafeXoshiro256ssRNG struct {
    // contains filtered or unexported fields
}

UnsafeXoshiro256ssRNG It is unsafe to call UnsafeRNG methods from concurrent goroutines.

UnsafeXoshiro256** is a pseudorandom number generator. For an interesting commentary on xoshiro256** https://www.pcg-random.org/posts/a-quick-look-at-xoshiro256.html

It is however very fast, and strong enough for most practical purposes

func NewUnsafeXoshiro256ssRNG Uses

func NewUnsafeXoshiro256ssRNG(seed int64) *UnsafeXoshiro256ssRNG

NewUnsafeXoshiro256ssRNG creates a new Thread unsafe PRNG generator

func (*UnsafeXoshiro256ssRNG) Seed Uses

func (r *UnsafeXoshiro256ssRNG) Seed(seed int64)

Seed takes a single uint64 and runs it through splitmix64 to seed the 256 bit starting state for the RNG

func (*UnsafeXoshiro256ssRNG) Uint64 Uses

func (r *UnsafeXoshiro256ssRNG) Uint64() uint64

Uint64 generates a random Uin64, (not thread safe)

Package fastrand64 imports 3 packages (graph). Updated 2020-10-29. Refresh now. Tools for package owners.