random

package module
v0.0.0-...-57ea5ee Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2020 License: MIT Imports: 7 Imported by: 2

README

random

go.dev Reference Build status Go Report Card

Utilities to complement Golang's math/rand package.

  • Interface to cover the methods of rand.Rand
  • Mock implementation of Interface
  • Locked implementation of Interface
  • Helpers for constructing implementations with different seeding methods

Documentation

Overview

Package random provides utilities to complement the math/rand package.

Index

Constants

This section is empty.

Variables

CryptoSeeder generates seeds using crypto/rand.

View Source
var UnixNanoSeeder = SeedFunc(func() (int64, error) {
	return time.Now().UnixNano(), nil
})

UnixNanoSeeder generates seeds with the current nanosecond epoch.

Functions

This section is empty.

Types

type Interface

type Interface interface {
	Seed(seed int64)
	ExpFloat64() float64
	Float32() float32
	Float64() float64
	Int() int
	Int31() int32
	Int31n(n int32) int32
	Int63() int64
	Int63n(n int64) int64
	Intn(n int) int
	NormFloat64() float64
	Perm(n int) []int
	Uint32() uint32
	io.Reader
}

Interface is an interface for random generators.

func Must

func Must(r Interface, err error) Interface

Must panics if err is non-nil and returns r otherwise.

func New

func New() Interface

New builds a new random generator (seeded with current nanosecond unix epoch).

func NewFromSeeder

func NewFromSeeder(s Seeder) (Interface, error)

NewFromSeeder builds a new random generator with a seed generated with s.

func NewLocked

func NewLocked(base Interface) Interface

NewLocked returns a random source that makes locked calls to the given base random source, making it safe for use with concurrent goroutines.

func NewWithSeed

func NewWithSeed(seed int64) Interface

NewWithSeed builds a random generator with the given seed.

type Mock

type Mock struct {
	mock.Mock
}

Mock is an autogenerated mock type for the Interface type

func (*Mock) ExpFloat64

func (m *Mock) ExpFloat64() float64

ExpFloat64 provides a mock function with given fields:

func (*Mock) Float32

func (m *Mock) Float32() float32

Float32 provides a mock function with given fields:

func (*Mock) Float64

func (m *Mock) Float64() float64

Float64 provides a mock function with given fields:

func (*Mock) Int

func (m *Mock) Int() int

Int provides a mock function with given fields:

func (*Mock) Int31

func (m *Mock) Int31() int32

Int31 provides a mock function with given fields:

func (*Mock) Int31n

func (m *Mock) Int31n(n int32) int32

Int31n provides a mock function with given fields: n

func (*Mock) Int63

func (m *Mock) Int63() int64

Int63 provides a mock function with given fields:

func (*Mock) Int63n

func (m *Mock) Int63n(n int64) int64

Int63n provides a mock function with given fields: n

func (*Mock) Intn

func (m *Mock) Intn(n int) int

Intn provides a mock function with given fields: n

func (*Mock) NormFloat64

func (m *Mock) NormFloat64() float64

NormFloat64 provides a mock function with given fields:

func (*Mock) Perm

func (m *Mock) Perm(n int) []int

Perm provides a mock function with given fields: n

func (*Mock) Read

func (m *Mock) Read(p []byte) (int, error)

Read provides a mock function with given fields: p

func (*Mock) Seed

func (m *Mock) Seed(seed int64)

Seed provides a mock function with given fields: seed

func (*Mock) Uint32

func (m *Mock) Uint32() uint32

Uint32 provides a mock function with given fields:

type ReaderSeeder

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

ReaderSeeder reads seeds from an underlying io.Reader.

func (*ReaderSeeder) Seed

func (r *ReaderSeeder) Seed() (int64, error)

Seed reads 8 bytes from the underlying reader and generates a seed from it.

type SeedFunc

type SeedFunc func() (int64, error)

SeedFunc is an adapter to allow ordinary functions to be used as Seeder implementations.

func ConstantSeeder

func ConstantSeeder(seed int64) SeedFunc

ConstantSeeder builds a SeedFunc that always returns seed.

func (SeedFunc) Seed

func (f SeedFunc) Seed() (int64, error)

Seed calls f.

type Seeder

type Seeder interface {
	Seed() (int64, error)
}

Seeder is a method of generating a seed.

func NewReaderSeeder

func NewReaderSeeder(r io.Reader) Seeder

NewReaderSeeder builds a ReaderSeeder reading from r.

Jump to

Keyboard shortcuts

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