godropbox: github.com/dropbox/godropbox/math2/rand2 Index | Files

package rand2

import "github.com/dropbox/godropbox/math2/rand2"

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

Package Files

rand.go

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 Uses

func Dur(max time.Duration) time.Duration

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

func GetSeed Uses

func GetSeed() int64

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

func Jitter Uses

func Jitter(period time.Duration) time.Duration

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

func JitterFraction Uses

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 Uses

func NewSource(seed int64) rand.Source

This returns a thread-safe random source.

func PickN Uses

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 Uses

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

Samples 'k' elements from the given slice

func SampleInts Uses

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

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

func Shuffle Uses

func Shuffle(collection Swapper)

Randomly shuffles the collection in place.

type Swapper Uses

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.

Package rand2 imports 7 packages (graph) and is imported by 19 packages. Updated 2016-11-06. Refresh now. Tools for package owners.