random

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2020 License: BSD-3-Clause Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bernoulli

func Bernoulli(p float64) bool

Bernoulli ...

func NewMasterseed

func NewMasterseed() ([32]byte, error)

NewMasterseed Compute a uniformly random series of 32 bytes.

Errors if the underlying generator does not have sufficient
entropy.

func NewNonce

func NewNonce() (uint64, error)

NewNonce Compute a uniformly random uint64.

Errors if the underlying generator does not have sufficient
entropy.

func Rand

func Rand(min, max int) int

Rand returns a number inside [min, max). Panics if min >= max

func Subset

func Subset(s Sampler, k int) []int

Subset creates a list of at most k unique numbers sampled from the sampler. Runs in O(k) * O(Sample) time with O(k) space used.

Types

type Sampler

type Sampler interface {
	Sample() int
	SampleReplace() int
	CanSample() bool
	Replace()
}

Sampler allows the sampling of integers

type Uniform

type Uniform struct {
	N int
	// contains filtered or unexported fields
}

Uniform implements the Sampler interface by using the uniform distribution in the range [0, N). All operations run in O(1) time.

func (*Uniform) CanSample

func (s *Uniform) CanSample() bool

CanSample implements the Sampler interface

func (*Uniform) Replace

func (s *Uniform) Replace()

Replace implements the Sampler interface

func (*Uniform) Sample

func (s *Uniform) Sample() int

Sample implements the Sampler interface

func (*Uniform) SampleReplace

func (s *Uniform) SampleReplace() int

SampleReplace implements the Sampler interface

type Weighted

type Weighted struct {
	Weights []uint64
	// contains filtered or unexported fields
}

Weighted implements the Sampler interface by sampling based on a heap structure.

Node weight is defined as the node's given weight along with it's children's recursive weights. Once sampled, a nodes given weight is set to 0.

Replacing runs in O(n) time while sampling runs in O(log(n)) time.

func (*Weighted) CanSample

func (s *Weighted) CanSample() bool

CanSample returns the number of items left that can be sampled

func (*Weighted) Replace

func (s *Weighted) Replace()

Replace all the sampled elements. Takes O(len(weights)) time.

func (*Weighted) Sample

func (s *Weighted) Sample() int

Sample returns a number in [0, len(weights)) with probability proportional to the weight of the item at that index. Assumes Len > 0. Sample takes O(log(len(weights))) time.

func (*Weighted) SampleReplace

func (s *Weighted) SampleReplace() int

SampleReplace returns a number in [0, len(weights)) with probability proportional to the weight of the item at that index. Assumes CanSample returns true. Sample takes O(log(len(weights))) time. The returned index is not removed.

Jump to

Keyboard shortcuts

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