randomizer

package
v2.1.0+incompatible Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2019 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package randomizer contains pseudo-random data generators, used by the Faker main package. It is concurrent safe, has 100% test coverage, benchmarks and examples. Can be used as a rand.* replacement.

RandWrapper encapsulates a rand.Rand but it is more user friendly and has more functions. It has 2 modes: * fast - for single-thread/goroutine performance requirements * safe - slower but safe for concurrent use (like rand.* it uses a mutex)

For more info and examples see https://github.com/bgadrian/fastfaker

Example
gen := NewRandWrapper(false, nil)
gen.Seed(42)

fmt.Printf("Int8: %d\n", gen.Int8())
fmt.Printf("Int16: %d\n", gen.Int16())
fmt.Printf("Int32: %d\n", gen.Int32())
fmt.Printf("Int32: %d\n", gen.Int64())
fmt.Printf("Int64Positive: %d\n", gen.Int64Positive())

fmt.Printf("Uint8: %d\n", gen.Uint8())
fmt.Printf("Uint16: %d\n", gen.Uint16())
fmt.Printf("Uint32: %d\n", gen.Uint32())

fmt.Printf("Float32: %f\n", gen.Float32())
fmt.Printf("Float64: %f\n", gen.Float64())
Output:

Int8: 49
Int16: 13387
Int32: -1805934616
Int32: -7297359450328151799
Int64Positive: 404153945743547657
Uint8: 97
Uint16: 23461
Uint32: 1276434112
Float32: 130343333431123176975039154477607157760.000000
Float64: 116198632802140183205174997036310832568880167563288775268797842968811055189411147761992698981182854609204521400932481006915558598725364731145245182600684556023951651432775097450784441328418806702862825300469152646818691585777068968631119723028318015329488476327072796506412228424374316456135062145352853356544.000000

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RandWrapper

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

RandWrapper encapsulates a rand.Rand and adds more features. Implements Randomizer. The only way to make it concurrent safe is to use NewRandWrapper(true, ...) Do not use this struct directly to avoid braking changes, use the Randomizer interface!

func (*RandWrapper) Float32

func (f *RandWrapper) Float32() float32

Float32 returns a random positive float number [0.000000000001, math.MaxFloat32)

func (*RandWrapper) Float32Range

func (f *RandWrapper) Float32Range(min, max float32) float32

Float32Range will generate a random float32 value between min and max

func (*RandWrapper) Float64

func (f *RandWrapper) Float64() float64

Float64 returns a random positive float number [0.000000000001, math.MaxFloat64)

func (*RandWrapper) Float64Range

func (f *RandWrapper) Float64Range(min, max float64) float64

Float64Range will generate a random float64 value between min and max

func (*RandWrapper) Float64Unary

func (f *RandWrapper) Float64Unary() float64

Float64Unary returns, as a float64, a pseudo-random number in [0.0,1.0) from the default Source.

func (*RandWrapper) Int16

func (f *RandWrapper) Int16() int16

Int16 will generate a random int16 value

func (*RandWrapper) Int32

func (f *RandWrapper) Int32() int32

Int32 will generate a random int32 value [math.MinInt32, math.MaxInt32)

func (*RandWrapper) Int32Range

func (f *RandWrapper) Int32Range(min, max int32) int32

Int32Range generates a random int32 between [min, max)

func (*RandWrapper) Int64

func (f *RandWrapper) Int64() int64

Int64 will generate a random int64 value [math.MinInt64, math.MaxInt64)

func (*RandWrapper) Int64Positive

func (f *RandWrapper) Int64Positive() int64

Int64Positive returns a non-negative pseudo-random 63-bit integer as an int64 [0, math.Maxint64)

func (*RandWrapper) Int64Range

func (f *RandWrapper) Int64Range(min, max int64) int64

Int64Range generates a random int64 between [min, max)

func (*RandWrapper) Int8

func (f *RandWrapper) Int8() int8

Int8 will generate a random Int8 value

func (*RandWrapper) Intn

func (f *RandWrapper) Intn(n int) int

Intn returns a random number between [0, n)

func (*RandWrapper) Number

func (f *RandWrapper) Number(min int, max int) int

Number will generate a random number between given min And max

func (*RandWrapper) Read

func (f *RandWrapper) Read(p []byte) (n int, err error)

Read generates len(p) random bytes from the default Source and writes them into p. It always returns len(p) and a nil error.

func (*RandWrapper) Seed

func (f *RandWrapper) Seed(seed int64)

Seed uses the provided seed value to initialize the generator to a deterministic state.

func (*RandWrapper) ShuffleInts

func (f *RandWrapper) ShuffleInts(a []int)

ShuffleInts will randomize a slice of ints

func (*RandWrapper) Uint16

func (f *RandWrapper) Uint16() uint16

Uint16 will generate a random uint16 value

func (*RandWrapper) Uint32

func (f *RandWrapper) Uint32() uint32

Uint32 will generate a random uint32 value

func (*RandWrapper) Uint64

func (f *RandWrapper) Uint64() uint64

Uint64 will generate a random uint64 value

func (*RandWrapper) Uint8

func (f *RandWrapper) Uint8() uint8

Uint8 will generate a random uint8 value

type Randomizer

type Randomizer interface {
	Seed(seed int64)
	Number(min int, max int) int
	Uint8() uint8
	Uint16() uint16
	Uint32() uint32
	Uint64() uint64
	Int8() int8
	Int16() int16
	Int32() int32
	Int64() int64
	Int64Positive() int64
	Int32Range(min, max int32) int32
	Int64Range(min, max int64) int64
	Float32Range(min, max float32) float32
	Float64Range(min, max float64) float64
	ShuffleInts(a []int)
	Intn(n int) int
	Float32() float32
	Float64() float64
	Read(p []byte) (n int, err error)
	Float64Unary() float64
}

Randomizer represents a base generator for the faker

func NewRandWrapper

func NewRandWrapper(concurrentSafe bool, source rand.Source) Randomizer

NewRandWrapper returns a wrapper of a rand.Rand with extra features. All functions are concurrent safe only if the param concurrentSafe==true If source is nil a new source will be generated with a random seed.

Jump to

Keyboard shortcuts

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