mongo-drivergo.mongodb.org/mongo-driver/internal/randutil Index | Files

package randutil

import "go.mongodb.org/mongo-driver/internal/randutil"

Package randutil provides common random number utilities.

Index

Functions

func CryptoSeed

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.

Source Files

randutil.go

Version
v1.9.2
Published
Oct 4, 2022
Platform
darwin/amd64
Imports
5 packages
Last checked
15 minutes ago

Tools for package owners.