rng

package
v0.3.26 Latest Latest
Warning

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

Go to latest
Published: Nov 25, 2021 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package rng implements a deterministic random number generator using cryptographic functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RandomSeed

func RandomSeed() string

RandomSeed returns a random 16-character string with 80 bits of entropy.

Types

type RNG

type RNG struct {
	// Count is the number of bytes RNG has been advanced by.
	//
	// This number is not read by any RNG calculations, and is provided
	// for debugging only.
	Count uint64

	// Flags to maintain compatibility with bugs in previous versions of RNG.
	SeparateUpdateSeed []byte
	ReverseFloat       bool
	MaxValueFencepost  bool
	MaxValueShift      bool
	// contains filtered or unexported fields
}

RNG is a deterministic random number generator.

func New

func New(newHash func() hash.Hash, seed string) *RNG

New constructs an RNG with a given hash function and seed.

func (*RNG) CheapFloat

func (r *RNG) CheapFloat() float64

CheapFloat returns a real number in [0, 1), advancing the random state by 1 byte.

func (*RNG) Clone

func (r *RNG) Clone() *RNG

Clone returns a full copy of this RNG.

Canges to one will not be reflected in the other.

func (*RNG) Float

func (r *RNG) Float() float64

Float returns a real number in [0, 1), advancing the random state by 4 bytes.

func (*RNG) RangeFloat

func (r *RNG) RangeFloat(min, max float64) float64

RangeFloat returns a real number in [min, max), advancing the random state by 4 bytes.

func (*RNG) RangeInt

func (r *RNG) RangeInt(min, max int) int

RangeInt returns an integer in [min, max), advancing the random state by a variable amount.

func (*RNG) RangeIntFloat

func (r *RNG) RangeIntFloat(min, max float64) float64

RangeIntFloat returns an integer in [min, max), advancing the random state by a variable amount.

This function matches the behavior of RangeInt for integer min and max, and matches the behavior of JavaScript for non-integers.

Non-integer min causes the result to have min's fractional part.

A non-integer difference between min and max causes the last number to have a lower probability of occurring.

Jump to

Keyboard shortcuts

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