rng

package
v0.0.0-...-c477d30 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2020 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package rng defines a modular pseudo-random number generation library. The randomizing routines are packaged into structs to allow them to be passed as function parameters. This provides better support for testing as alternate implementations can be passed which produce "random" behavior which is predictable enough for complex unit testing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ManualRand

type ManualRand struct {
	// contains filtered or unexported fields
}

ManualRand defines a RandomSource which is manually fed random values to be retrieved. This is very useful for taking complete control of random number generation for unit testing.

func UseManual

func UseManual(v ...float64) *ManualRand

UseManual creates a new ManualRand containing the supplied values.

func (*ManualRand) Add

func (r *ManualRand) Add(v ...float64)

Add adds new values to the queue of values to supply via the Next function.

func (*ManualRand) Clear

func (r *ManualRand) Clear()

Clear removes all stored values in the random queue.

func (*ManualRand) Next

func (r *ManualRand) Next() float64

Next retrieves the next value in the queue of random numbers. If no values have been stored in buffer, then the function panics.

type RandomSource

type RandomSource interface {
	// Next retrieves a psudo-random floatin-point number, such that 0 <= n < 1
	Next() float64
}

RandomSource defines an interface which can be used to supply data to code performing pseudo-random behaviors.

type StaticRand

type StaticRand struct {
	// contains filtered or unexported fields
}

StaticRand is a RandomSource that always returns the same value. This is a simple way of supporting RandomSources for testing when a single static value is acceptable.

func UseStatic

func UseStatic(v float64) *StaticRand

UseStatic creates a new StaticRand which will continually supply the given value.

func (*StaticRand) Next

func (s *StaticRand) Next() float64

Next returns the statically defined random value.

type SystemRand

type SystemRand struct {
}

SystemRand defines a RandomSource that uses the native operating system's random number generation.

func UseSystem

func UseSystem() *SystemRand

UseSystem creates a new SystemRand.

func (*SystemRand) Next

func (s *SystemRand) Next() float64

Next returns a new pseudo-random number from the operating systems random number source.

Jump to

Keyboard shortcuts

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