Help us shape the future of Go! Take the 2026 Go Developer Survey

randutil

package module
v0.0.0-...-4f7dd65 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2019 License: MIT Imports: 9 Imported by: 0

README

[DEPRECATED] Use golang.org/x/exp/rand#PCGSource and gonum.org/v1/gonum/stat/distuv instead.

randutil GoDoc

This is a Go package for providing some utilities for random.

This Package randutil provides a utility function to get non-negative pesudo-random int using math/rand or crypto/rand.

This package also provides Chooser which chooses an item from choices. Each choice has a weight which affects possibility for it to be choosed.

License

MIT

Documentation

Overview

Package randutil provides a utility function to get non-negative pesudo-random int using math/rand or crypto/rand.

This package also provides Chooser which chooses an item from choices. Each choice has a weight which affects possibility for it to be choosed.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Erlang

func Erlang(intner Intner, lambda float64, k int) (float64, error)

Erlang returns an erlang distributed random value.

func Exp

func Exp(intner Intner, lambda float64) (float64, error)

Exp returns an exponential distributed random value.

func Float64

func Float64(intner Intner) (float64, error)

Float64 returns, as a float64, a pseudo-random number in [0.0,1.0).

func NormFloat64

func NormFloat64(intner Intner) (float64, error)

NormFloat64 returns a normally distributed float64 in the range [-math.MaxFloat64, +math.MaxFloat64] with standard normal distribution (mean = 0, stddev = 1). To produce a different normal distribution, callers can adjust the output using:

sample = NormFloat64() * desiredStdDev + desiredMean

func Poisson

func Poisson(intner Intner, lambda float64) (int, error)

Poisson returns a poisson distributed random value.

func RandomSeed

func RandomSeed() (int64, error)

RandomSeed returns a random seed using the cryptographically secure pseudo random number generator.

Types

type Choice

type Choice struct {
	Weight uint
	Item   interface{}
}

Choice represents an item to be choosed. It has a weight which affects possibility to be choosed.

type Chooser

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

Chooser represents a chooser that chooses an item randomly.

func NewChooser

func NewChooser(intner Intner, choices []Choice) (*Chooser, error)

NewChooser returns a new chooser for choices. It returns an error if total weight of choices is not positive.

Example
package main

import (
	"fmt"

	"github.com/hnakamur/randutil"
)

func main() {
	c, err := randutil.NewChooser(
		randutil.NewCryptoIntner(),
		[]randutil.Choice{
			// This is a cheated coin that possibility of tail is twice of head.
			{Weight: 1, Item: "head"},
			{Weight: 2, Item: "tail"},
		},
	)
	if err != nil {
		panic(err)
	}

	counts := map[string]int{
		"head": 0,
		"tail": 0,
	}
	for i := 0; i < 10000; i++ {
		v, err := c.Choose()
		if err != nil {
			panic(err)
		}
		counts[v.(string)]++
	}
	fmt.Printf("head=%d, tail=%d\n", counts["head"], counts["tail"])
}

func (*Chooser) Choose

func (c *Chooser) Choose() (interface{}, error)

Choose returns an randomly selected item from choices. It returns an error if the underlying intner returned en error.

type CubicNoise

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

func NewCubicNoise

func NewCubicNoise(seed, octave, periodX, periodY int32) *CubicNoise

func (*CubicNoise) Sample1D

func (n *CubicNoise) Sample1D(x float64) float64

type Encoder

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

Encoder generates random values and encodes them in the specified digit characters.

func NewEncoder

func NewEncoder(intner Intner, digits []byte) (*Encoder, error)

NewEncoder creates an Encoder. The length of digits must be between 1 and 256.

func (*Encoder) RandomBytes

func (e *Encoder) RandomBytes(buf []byte) error

Bytes writes random encoded bytes to the passed buffer.

func (*Encoder) RandomString

func (e *Encoder) RandomString(length int) (string, error)

String returns a random encoded string of the specified length.

type General

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

General is a general discrete distributed random value generator.

func NewGeneral

func NewGeneral(intner Intner, g []float64) (*General, error)

NewGeneral creates a general discrete distributed random value generator.

The rules for g are: * The lenght of g must be greater than or equal to 2. * The first element of g must be 0. * The last element of g must be 1. * The g must be monotonously increasing, that is, g[i] >= g[i-1] for any i.

func (*General) Next

func (g *General) Next() (float64, error)

Next returns a general discrete distributed random value.

type Intner

type Intner interface {
	Intn(n int) (int, error)
	Int63n(n int64) (int64, error)
}

Intner is the interface that returns a non-negative pseudo random int in the specified range.

Intn returns, as an int, a non-negative pseudo-random number in [0,n). It panics if n <= 0.

Int63n returns, as an int64, a non-negative pseudo-random number in [0,n) from the default Source. It panics if n <= 0.

func NewCryptoIntner

func NewCryptoIntner() Intner

NewCryptoIntner returns a new Intner using crypto/rand.

Example
package main

import (
	"fmt"

	"github.com/hnakamur/randutil"
)

func main() {
	intner := randutil.NewCryptoIntner()
	for i := 0; i < 10; i++ {
		v, err := intner.Intn(6)
		if err != nil {
			panic(err)
		}
		fmt.Printf("dice was %d\n", v+1)
	}
}

func NewMathIntner

func NewMathIntner(seed int64) Intner

NewMathIntner returns a new Intner using math/rand. This Intner is not safe for concurrent use by multiple goroutines.

Example
package main

import (
	"fmt"
	"time"

	"github.com/hnakamur/randutil"
)

func main() {
	intner := randutil.NewMathIntner(time.Now().UnixNano())
	for i := 0; i < 10; i++ {
		v, err := intner.Intn(6)
		if err != nil {
			panic(err)
		}
		fmt.Printf("dice was %d\n", v+1)
	}
}

func NewPCG32Intner

func NewPCG32Intner(seed uint64) Intner

NewPCG32Intner returns a new Intner using pcg.Pcg32.

func NewPCG64Intner

func NewPCG64Intner(seedH, seedL uint64) Intner

NewPCG64Intner returns a new Intner using pcg.Pcg64.

func NewXorShift64StarIntner

func NewXorShift64StarIntner(seed uint64) Intner

NewXorShift64StarIntner returns a new Intner using xorshift.XorShift64Star.

func NewXorShift128PlusIntner

func NewXorShift128PlusIntner(seed uint64) Intner

NewXorShift128PlusIntner returns a new Intner using xorshift.XorShift128Plus.

func NewXorShift1024StarIntner

func NewXorShift1024StarIntner(seed uint64) Intner

NewXorShift1024StarIntner returns a new Intner using xorshift.XorShift1024Star.

Example
package main

import (
	"fmt"
	"time"

	"github.com/hnakamur/randutil"
)

func main() {
	intner := randutil.NewXorShift1024StarIntner(uint64(time.Now().UnixNano()))
	for i := 0; i < 10; i++ {
		v, err := intner.Intn(6)
		if err != nil {
			panic(err)
		}
		fmt.Printf("dice was %d\n", v+1)
	}
}

type Reader

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

Reader is a type for reading random bytes.

func NewReader

func NewReader(i Intner) *Reader

NewReader creates a new random reader.

func (*Reader) Read

func (r *Reader) Read(p []byte) (int, error)

Read reads random bytes to the buffer.

Directories

Path Synopsis
Package randutil/v2 provides a utility function to produce random integer, float, bytes, and texts.
Package randutil/v2 provides a utility function to produce random integer, float, bytes, and texts.

Jump to

Keyboard shortcuts

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