gopherng

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2023 License: Apache-2.0 Imports: 6 Imported by: 0

README

gopherng

Seeded pseudorandom float64 generator and Reader

Go Reference

Note

This PRNG was implemented for the purposes of the CubeDance game and may not be applicable for your purposes.

Pseudorandom float64 values

// Initialize f64 PRNG with seed. (Use a high-entropy one IRL.)
p := gopherng.NewFloat64PRNG([]byte{1, 2, 3, 4, 5})

// Generate values.
v, err := p.Next()
if err != nil {/*handle error if any*/}
fmt.Printf("%f\n", v)

// ... Keep using p.Next() to generate additional values.

Pseudorandom Reader

This package additionally includes a pseudorandom Reader. It initializes, based on a provided seed, an XChaCha20 stream cipher which is used to encrypt 0s in order to generate random bytes.

You can use it like so:

// Initialize pseudorandom reader with a seed. (Use a high-entropy one IRL.)
r := gopherng.NewPseudorandomReader([]byte{1, 2, 3, 4, 5})

// And Read from r!

Notes

This package relies upon rand.Int() from crypto/rand reading a consistent number of bytes for each Int generated from a gopherng.PseudorandomReader in order to generate consistent output from Float64PRNGs.

Use a large, high-entropy seed if you want to make the seed more difficult to figure when exposing random values.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Float64PRNG

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

Float64PRNG is a seeded pseudorandom float64 generator using a seeded PseudorandomReader initialized from a provided seed.

func NewFloat64PRNG

func NewFloat64PRNG(seed []byte) *Float64PRNG

NewFloat64PRNG returns a new Float64PRNG with a PseudorandomReader initialized using seed.

func (*Float64PRNG) Next

func (f *Float64PRNG) Next() (float64, error)

Next gets the next float64 from the Float64PRNG.

type PseudorandomReader added in v1.0.0

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

PseudorandomReader is a Reader providing seeded pseudorandom output from an internal stream cipher encrypting 0s.

It is not concurrency-safe.

func NewPseudorandomReader added in v1.0.0

func NewPseudorandomReader(seed []byte) *PseudorandomReader

NewPseudorandomReader initializes a PseudorandomReader by initializing its internal stream cipher and generating the first buffer.

func (*PseudorandomReader) Read added in v1.0.0

func (p *PseudorandomReader) Read(b []byte) (int, error)

Read reads random bytes. It should always read len(b) bytes without error.

Jump to

Keyboard shortcuts

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