rng

package
v0.2.71 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2020 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

View Source
var ForceSeed string

ForceSeed is a user-specified seed to use.

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

	// ReverseFloat generates float values in big-endian order to
	// maintain compatibility with a bug in previous versions of RNG.
	ReverseFloat bool
	// contains filtered or unexported fields
}

RNG is a deterministic random number generator.

func New

func New(hash 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.

It is not safe to use both RNG instances concurrently, but changes 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