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

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.8.0
Published
Nov 23, 2021
Platform
linux/amd64
Imports
2 packages
Last checked
14 seconds ago

Tools for package owners.