randutil

package
v1.8.6 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2022 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package randutil provides common random number utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CryptoSeed added in v1.8.5

func CryptoSeed() int64

CryptoSeed returns a random int64 read from the "crypto/rand" random number generator. It is intended to be used to seed pseudorandom number generators at package initialization. It panics if it encounters any errors.

Types

type LockedRand

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

A LockedRand wraps a "math/rand".Rand and is safe to use from multiple goroutines.

func NewLockedRand

func NewLockedRand(src rand.Source) *LockedRand

NewLockedRand returns a new LockedRand that uses random values from src to generate other random values. It is safe to use from multiple goroutines.

func (*LockedRand) Intn

func (lr *LockedRand) 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 (*LockedRand) Read

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

Read generates len(p) random bytes and writes them into p. It always returns len(p) and a nil error.

func (*LockedRand) Shuffle

func (lr *LockedRand) Shuffle(n int, swap func(i, j int))

Shuffle pseudo-randomizes the order of elements. n is the number of elements. Shuffle panics if n < 0. swap swaps the elements with indexes i and j.

Note that Shuffle locks the LockedRand, so shuffling large collections may adversely affect other concurrent calls. If many concurrent Shuffle and random value calls are required, consider using the global "math/rand".Shuffle instead because it uses much more granular locking.

Jump to

Keyboard shortcuts

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