grand

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2023 License: MIT Imports: 4 Imported by: 1

README

Go Random Generator

Build Coverage Go Report Card Go Reference

Grand is a simple random generator wrapper on math/rand seeded by entropy from crypto/rand

Usage

Create a source that can be used by the randomizer. NewSource creates a source with an optional seed. If there is no seed provided, a truly random seed will be derived from crypto/rand.

If a seed is provided, the function will not return an error. If entropy is used, the function can return an error if it fails to read bytes from crypto/rand

// New source using entropy
src, err := grand.NewSource()

// New source from existing seed
src, _ := grand.NewSource(1552664635341843643)

The randomizer can use any math/rand source, not only ones created using this package.

// New randomizer from source
rng := grand.New(src)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewSource

func NewSource(seed ...int64) (rand.Source, error)

NewSource creates a random source. If a seed is given, it is used to seed the random generator, otherwise entropy data from a crypto/rand is used as seed.

Types

type Random

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

Random is a wrapper around a math/rand random generator

func New

func New(src rand.Source) *Random

New creates a random generator from a source.

func (*Random) ExpFloat64

func (r *Random) ExpFloat64() float64

ExpFloat64 calls ExpFloat64 from math/rand

func (*Random) Fill

func (r *Random) Fill(dest []byte) []byte

Fill generates random data to fill a byte array to its length. It works similarly to Read([]byte) from math/rand, except it's thread safe and it does not return an error to be handled

func (*Random) Float32

func (r *Random) Float32() float32

Float32 calls Float32 from math/rand

func (*Random) Float64

func (r *Random) Float64() float64

Float64 calls Float64 from math/rand

func (*Random) Int

func (r *Random) Int() int

Int calls Int from math/rand

func (*Random) Int31

func (r *Random) Int31() int32

Int31 calls Int31 from math/rand

func (*Random) Int31n

func (r *Random) Int31n(n int32) int32

Int31n calls Int31n from math/rand

func (*Random) Int63

func (r *Random) Int63() int64

Int63 calls Int63 from math/rand

func (*Random) Int63n

func (r *Random) Int63n(n int64) int64

Int63n calls Int63n from math/rand

func (*Random) Intn

func (r *Random) Intn(n int) int

Intn calls Intn from math/rand

func (*Random) NormFloat64

func (r *Random) NormFloat64() float64

NormFloat64 calls NormFloat64 from math/rand

func (*Random) Perm

func (r *Random) Perm(n int) []int

Perm calls Perm from math/rand

func (*Random) Read

func (r *Random) Read(p []byte) (n int, err error)

Read calls Read from math/rand

func (*Random) Seed

func (r *Random) Seed(seed int64)

Seed calls Seed from math/rand

func (*Random) Shuffle

func (r *Random) Shuffle(n int, swap func(i, j int))

Shuffle calls Shuffle from math/rand

func (*Random) Uint32

func (r *Random) Uint32() uint32

Uint32 calls Uint32 from math/rand

func (*Random) Uint64

func (r *Random) Uint64() uint64

Uint64 calls Uint64 from math/rand

Jump to

Keyboard shortcuts

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