rand

package
v0.25.0 Latest Latest
Warning

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

Go to latest
Published: Jun 1, 2021 License: MIT Imports: 3 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LockedRand

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

LockedRand implements NumberGenerator and embeds Rand that is safe for concurrent use.

func NewLockedRand

func NewLockedRand(seed int64) *LockedRand

NewLockedRand creates a new LockedRand that implements all Rand functions that is safe for concurrent use.

func (*LockedRand) Float32

func (lr *LockedRand) Float32() (n float32)

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

func (*LockedRand) Float64

func (lr *LockedRand) Float64() (n float64)

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

func (*LockedRand) Int

func (lr *LockedRand) Int() (n int)

Int returns a non-negative pseudo-random int.

func (*LockedRand) Int31

func (lr *LockedRand) Int31() (n int32)

Int31 returns a non-negative pseudo-random 31-bit integer as an int32.

func (*LockedRand) Int31n

func (lr *LockedRand) Int31n(n int32) (r int32)

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

func (*LockedRand) Int63

func (lr *LockedRand) Int63() (n int64)

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

func (*LockedRand) Int63n

func (lr *LockedRand) Int63n(n int64) (r int64)

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

func (*LockedRand) Intn

func (lr *LockedRand) Intn(n int) (r int)

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

func (*LockedRand) Perm

func (lr *LockedRand) Perm(n int) (r []int)

Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n).

func (*LockedRand) Read

func (lr *LockedRand) Read(p []byte) (n int, err error)

Read generates len(p) random bytes and writes them into p. It always returns len(p) and a nil error. Read should not be called concurrently with any other Rand method.

func (*LockedRand) Seed

func (lr *LockedRand) Seed(seed int64)

Seed uses the provided seed value to initialize the generator to a deterministic state. Seed should not be called concurrently with any other Rand method.

func (*LockedRand) TwoInt63

func (lr *LockedRand) TwoInt63() (n1, n2 int64)

TwoInt63 generates 2 random int64 without locking twice.

func (*LockedRand) TwoUint64 added in v0.23.0

func (lr *LockedRand) TwoUint64() (n1, n2 uint64)

TwoUint64 generates 2 random uint64 without locking twice.

func (*LockedRand) Uint32

func (lr *LockedRand) Uint32() (n uint32)

Uint32 returns a pseudo-random 32-bit value as a uint32.

func (*LockedRand) Uint64

func (lr *LockedRand) Uint64() (n uint64)

Uint64 returns a pseudo-random 64-bit value as a uint64.

type NumberGenerator

type NumberGenerator interface {
	Uint64() uint64
	TwoUint64() (uint64, uint64)
}

NumberGenerator defines an interface to generate numbers.

type Pool

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

Pool represents a pool of random number generators. To generate a random id, round robin through the source pool with atomic increment. With more and more goroutines, Pool improves the performance of Random vs naive global random mutex exponentially. Try tests with 20000 goroutines and 500 calls to observe the difference

func NewPool

func NewPool(seed int64, size uint64) *Pool

NewPool takes in a size and creates a pool of random id generators with size equal to next closest power of 2. eg: NewPool(10) returns a pool with 2^4 = 16 random sources.

func (*Pool) Pick

func (r *Pool) Pick() NumberGenerator

Pick returns a NumberGenerator from a pool of NumberGenerators

Jump to

Keyboard shortcuts

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