Back to godoc.org
github.com/dropbox/godropbox/math2/rand2

package rand2

v0.0.0 (52ad444)
Latest Go to latest
Published: Feb 28, 2020 | License: BSD-3-Clause | Module: github.com/dropbox/godropbox

Overview

rand2 is a drop-in replacement for the "math/rand" package. It initializes the global random generator with a random seed (instead of 1), and provides additional functionality over the standard "math/rand" package.

Index

Variables

var (
	// See math/rand for documentation.
	New = rand.New

	// See math/rand for documentation.
	Seed = rand.Seed

	// See math/rand for documentation.
	Int63 = rand.Int63

	// See math/rand for documentation.
	Uint32 = rand.Uint32

	// See math/rand for documentation.
	Int31 = rand.Int31

	// See math/rand for documentation.
	Int = rand.Int

	// See math/rand for documentation.
	Int63n = rand.Int63n

	// See math/rand for documentation.
	Int31n = rand.Int31n

	// See math/rand for documentation.
	Intn = rand.Intn

	// See math/rand for documentation.
	Float64 = rand.Float64

	// See math/rand for documentation.
	Float32 = rand.Float32

	// See math/rand for documentation.
	Perm = rand.Perm

	// See math/rand for documentation.
	NormFloat64 = rand.NormFloat64

	// See math/rand for documentation.
	ExpFloat64 = rand.ExpFloat64

	// See math/rand for documentation.
	NewZipf = rand.NewZipf
)

func Dur

func Dur(max time.Duration) time.Duration

Dur returns a pseudo-random Duration in [0, max)

func GetSeed

func GetSeed() int64

Generates a seed based on the current time and the process ID.

func Jitter

func Jitter(period time.Duration) time.Duration

Uniformly jitters the provided duration by +/- 50%.

func JitterFraction

func JitterFraction(period time.Duration, fraction float64) time.Duration

Uniformly jitters the provided duration by +/- the given fraction. NOTE: fraction must be in (0, 1].

func NewSource

func NewSource(seed int64) rand.Source

This returns a thread-safe random source.

func PickN

func PickN(population []interface{}, n int) (
	picked []interface{}, remaining []interface{}, err error)

Same as 'Sample' except it returns both the 'picked' sample set and the 'remaining' elements.

func Sample

func Sample(population []interface{}, k int) (res []interface{}, err error)

Samples 'k' elements from the given slice

func SampleInts

func SampleInts(n int, k int) (res []int, err error)

Samples 'k' unique ints from the range [0, n)

func Shuffle

func Shuffle(collection Swapper)

Randomly shuffles the collection in place.

type Swapper

type Swapper interface {
	// Len is the number of elements in the collection.
	Len() int
	// Swap swaps the elements with indexes i and j.
	Swap(i int, j int)
}

A subset of sort.Interface used for random shuffle.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
f or F : Jump to identifier