rng

package module
Version: v0.0.0-...-a612b04 Latest Latest
Warning

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

Go to latest
Published: May 31, 2019 License: Apache-2.0 Imports: 4 Imported by: 25

README

go_rng

Build Status CircleCI GitHub stars GitHub license GoDoc

A pseudo-random number generator written in Golang v1.3 伪随机数生成器库的Go语言实现

Features

Inspired by:
Supported Distributions and Functionalities:

均匀分布 Uniform Distribution
伯努利分布 Bernoulli Distribution
卡方分布 Chi-Squared Distribution
Gamma分布 Gamma Distribution
Beta分布 Beta Distribution
费舍尔F分布 Fisher's F Distribution
柯西分布 Cauchy Distribution
韦伯分布 Weibull Distribution
Pareto分布 Pareto Distribution
对数高斯分布 Log Normal Distribution
指数分布 Exponential Distribution
学生T分布 Student's t-Distribution
二项分布 Binomial Distribution
泊松分布 Poisson Distribution
几何分布 Geometric Distribution
高斯分布 Gaussian Distribution
逻辑分布 Logistic Distribution
狄利克雷分布 Dirichlet Distribution

Requirements

  • Golang 1.7 and above

Installation

go get -u -v github.com/leesper/go_rng

Usage

func TestGaussianGenerator(t *testing.T) {
	fmt.Println("=====Testing for GaussianGenerator begin=====")
	grng := NewGaussianGenerator(time.Now().UnixNano())
	fmt.Println("Gaussian(5.0, 2.0): ")
	hist := map[int64]int{}
	for i := 0; i < 10000; i++ {
		hist[int64(grng.Gaussian(5.0, 2.0))]++
	}

	keys := []int64{}
	for k := range hist {
		keys = append(keys, k)
	}
	SortInt64Slice(keys)

	for _, key := range keys {
		fmt.Printf("%d:\t%s\n", key, strings.Repeat("*", hist[key]/200))
	}

	fmt.Println("=====Testing for GaussianGenerator end=====")
	fmt.Println()
}

output:

=====Testing for GaussianGenerator begin=====
Gaussian(5.0, 2.0):
-2:
-1:
0:	*
1:	**
2:	****
3:	*******
4:	*********
5:	*********
6:	*******
7:	****
8:	**
9:
10:
11:
12:
=====Testing for GaussianGenerator end=====

Authors and acknowledgment

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change. Please make sure to update tests as appropriate.

Documentation

Overview

Package rng implements a series of pseudo-random number generator based on a variety of common probability distributions

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BernoulliGenerator

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

UniformGenerator is a random number generator for uniform distribution. The zero value is invalid, use NewBernoulliGenerator to create a generator

func NewBernoulliGenerator

func NewBernoulliGenerator(seed int64) *BernoulliGenerator

NewBernoulliGenerator returns a bernoulli-distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: beng := rng.NewBernoulliGenerator(time.Now().UnixNano())

func (BernoulliGenerator) Bernoulli

func (beng BernoulliGenerator) Bernoulli() bool

Bernoulli returns a bool, which is true with probablity 0.5

func (BernoulliGenerator) Bernoulli_P

func (beng BernoulliGenerator) Bernoulli_P(p float64) bool

Bernoulli_P returns a bool, which is true with probablity p

type BetaGenerator

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

BetaGenerator is a random number generator for beta distribution. The zero value is invalid, use NewBetaGenerator to create a generator

func NewBetaGenerator

func NewBetaGenerator(seed int64) *BetaGenerator

NewBetaGenerator returns a beta distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: brng := rng.NewBetaGenerator(time.Now().UnixNano())

func (BetaGenerator) Beta

func (brng BetaGenerator) Beta(alpha, beta float64) float64

Beta returns a random number of beta distribution (alpha > 0.0 and beta > 0.0)

type BinomialGenerator

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

BinomialGenerator is a random number generator for binomial distribution. The zero value is invalid, use NewBinomialGenerator to create a generator

func NewBinomialGenerator

func NewBinomialGenerator(seed int64) *BinomialGenerator

NewBinomialGenerator returns a binomial-distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: bing := rng.NewBinomialGenerator(time.Now().UnixNano())

func (BinomialGenerator) Binomial

func (bing BinomialGenerator) Binomial(n int64, p float64) int64

Binomial returns a random number X ~ binomial(n, p)

type CauchyGenerator

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

CauchyGenerator is a random number generator for cauchy distribution. The zero value is invalid, use NewCauchyGenerator to create a generator

func NewCauchyGenerator

func NewCauchyGenerator(seed int64) *CauchyGenerator

NewCauchyGenerator returns a cauchy-distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: crng := rng.NewCauchyGenerator(time.Now().UnixNano())

func (CauchyGenerator) Cauchy

func (crng CauchyGenerator) Cauchy(x0, gamma float64) float64

Cauchy returns a random number of cauchy distribution

func (CauchyGenerator) StandardCauchy

func (crng CauchyGenerator) StandardCauchy() float64

StandardCauchy() returns a random number of standard cauchy distribution (x0 = 0.0, gamma = 1.0)

type ChiSquaredGenerator

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

ChiSquaredGenerator is a random number generator for chi-squared distribution. The zero value is invalid, use NewChiSquaredGenerator to create a generator

func NewChiSquaredGenerator

func NewChiSquaredGenerator(seed int64) *ChiSquaredGenerator

NewChiSquaredGenerator returns a chi-squared distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: crng := rng.NewChiSquaredGenerator(time.Now().UnixNano())

func (ChiSquaredGenerator) ChiSquared

func (crng ChiSquaredGenerator) ChiSquared(freedom int64) float64

ChiSquared returns a random number of chi-squared distribution (freedom > 0)

type DirichletGenerator

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

DirichletGenerator is a random number generator for dirichlet distribution. The zero value is invalid, use NewDirichletGenerator to create a generator

func NewDirichletGenerator

func NewDirichletGenerator(seed int64) *DirichletGenerator

NewDirichletGenerator returns a dirichlet-distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: drng := rng.NewDirichletGenerator(time.Now().UnixNano())

func (DirichletGenerator) Dirichlet

func (drng DirichletGenerator) Dirichlet(alphas []float64) []float64

Dirichlet returns random numbers of dirichlet distribution (alpha > 0.0, for alpha in alphas)

func (DirichletGenerator) FlatDirichlet

func (drng DirichletGenerator) FlatDirichlet(n int) []float64

FlatDirichlet returns random numbers of flat-dirichlet distribution (n > 0)

func (DirichletGenerator) SymmetricDirichlet

func (drng DirichletGenerator) SymmetricDirichlet(alpha float64, n int) []float64

SymmetricDirichlet returns random numbers of symmetric-dirichlet distribution (alpha > 0.0 and n > 0)

type ExpGenerator

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

ExpGenerator is a random number generator for exponential distribution. The zero value is invalid, use NewExpGenerator to create a generator

func NewExpGenerator

func NewExpGenerator(seed int64) *ExpGenerator

NewExpGenerator returns a exponential-distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: erng := rng.NewExpGenerator(time.Now().UnixNano())

func (ExpGenerator) Exp

func (erng ExpGenerator) Exp(lambda float64) float64

Exp returns a random number of exponential distribution

type FisherFGenerator

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

FisherFGenerator is a random number generator for Fisher's F distribution. The zero value is invalid, use NewFisherFGenerator to create a generator

func NewFisherFGenerator

func NewFisherFGenerator(seed int64) *FisherFGenerator

NewFisherFGenerator returns a Fisher's F distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: frng := rng.NewFisherFGenerator(time.Now().UnixNano())

func (FisherFGenerator) Fisher

func (frng FisherFGenerator) Fisher(d1, d2 int64) float64

Fisher returns a random number of Fisher's F distribution (d1 > 0 and d2 > 0)

type GammaGenerator

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

GammaGenerator is a random number generator for gamma distribution. The zero value is invalid, use NewGammaGenerator to create a generator

func NewGammaGenerator

func NewGammaGenerator(seed int64) *GammaGenerator

NewGammaGenerator returns a gamma distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: grng := rng.NewGammaGenerator(time.Now().UnixNano())

func (GammaGenerator) Gamma

func (grng GammaGenerator) Gamma(alpha, beta float64) float64

Gamma returns a random number of gamma distribution (alpha > 0.0 and beta > 0.0)

type GaussianGenerator

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

GaussianGenerator is a random number generator for gaussian distribution. The zero value is invalid, use NewGaussianGenerator to create a generator

func NewGaussianGenerator

func NewGaussianGenerator(seed int64) *GaussianGenerator

NewGaussianGenerator returns a gaussian-distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: crng := rng.NewGaussianGenerator(time.Now().UnixNano())

func (GaussianGenerator) Gaussian

func (grng GaussianGenerator) Gaussian(mean, stddev float64) float64

Gaussian returns a random number of gaussian distribution Gauss(mean, stddev^2)

func (GaussianGenerator) StdGaussian

func (grng GaussianGenerator) StdGaussian() float64

StdGaussian returns a random number of standard gaussian distribution

type GeometricGenerator

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

GeometricGenerator is a random number generator for geometric distribution. The zero value is invalid, use NewGeometryGenerator to create a generator

func NewGeometricGenerator

func NewGeometricGenerator(seed int64) *GeometricGenerator

NewGeometricGenerator returns a geometric-distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: grng := rng.NewGeometricGenerator(time.Now().UnixNano())

func (GeometricGenerator) Geometric

func (grng GeometricGenerator) Geometric(p float64) int64

Geometric returns a random number X ~ binomial(n, p)

type LogisticGenerator

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

LogisticGenerator is a random number generator for logistic distribution. The zero value is invalid, use NewLogisticGenerator to create a generator

func NewLogisticGenerator

func NewLogisticGenerator(seed int64) *LogisticGenerator

NewLogisticGenerator returns a logistic-distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: lrng := rng.NewLogisticGenerator(time.Now().UnixNano())

func (LogisticGenerator) Logistic

func (lrng LogisticGenerator) Logistic(mu, s float64) float64

Logistic returns a random number of logistic distribution

type LognormalGenerator

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

LognormalGenerator is a random number generator for lognormal distribution. The zero value is invalid, use NewLognormalGenerator to create a generator

func NewLognormalGenerator

func NewLognormalGenerator(seed int64) *LognormalGenerator

NewLognormalGenerator returns a lognormal-distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: crng := rng.NewLognormalGenerator(time.Now().UnixNano())

func (LognormalGenerator) Lognormal

func (lnng LognormalGenerator) Lognormal(mean, stddev float64) float64

Lognormal return a random number of lognormal distribution

type ParetoGenerator

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

ParetoGenerator is a random number generator for type I pareto distribution. The zero value is invalid, use NewParetoGenerator to create a generator

func NewParetoGenerator

func NewParetoGenerator(seed int64) *ParetoGenerator

NewParetoGenerator returns a type I pareto-distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: crng := rng.NewParetoGenerator(time.Now().UnixNano())

func (ParetoGenerator) Pareto

func (prng ParetoGenerator) Pareto(alpha float64) float64

Pareto returns a random number of type I pareto distribution (alpha > 0,0)

type PoissonGenerator

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

PoissonGenerator is a random number generator for possion distribution. The zero value is invalid, use NewPoissonGenerator to create a generator

func NewPoissonGenerator

func NewPoissonGenerator(seed int64) *PoissonGenerator

NewPoissonGenerator returns a possion-distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: prng := rng.NewPoissonGenerator(time.Now().UnixNano())

func (PoissonGenerator) Poisson

func (prng PoissonGenerator) Poisson(lambda float64) int64

Poisson returns a random number of possion distribution

type StudentTGenerator

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

StudentTGenerator is a random number generator for student-t distribution. The zero value is invalid, use NewStudentTGenerator to create a generator

func NewStudentTGenerator

func NewStudentTGenerator(seed int64) *StudentTGenerator

NewStudentTGenerator returns a student-t distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: stng := rng.NewStudentTGenerator(time.Now().UnixNano())

func (StudentTGenerator) Student

func (stng StudentTGenerator) Student(freedom int64) float64

Student returns a random number of student-t distribution (freedom > 0.0)

type TriangularGenerator

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

TriangularGenerator is a random number generator for Triangular distribution.

func NewTriangularGenerator

func NewTriangularGenerator(seed int64) *TriangularGenerator

NewTriangularGenerator returns a Triangular-distribution generator

func (TriangularGenerator) TriangularObj

func (Trng TriangularGenerator) TriangularObj(a, b, c float64) float64

TriangularObj returns value when x in[a,c] then cdf = (x-a)^2/((b-a)(c-a)) ,when x in(c,b] then 1 - (b-x)^2/((b-a)(b-c)) when x in[0,(c-a)/(b-a)] then invcdf = a+ sqrt(x*(b-1)(c-a)) ,when x in((c-a)/(b-a),1] then invcdf = b - sqrt((1-x)*(b-a)*(b-c))

type UniformGenerator

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

UniformGenerator is a random number generator for uniform distribution. The zero value is invalid, use NewUniformGenerator to create a generator

func NewUniformGenerator

func NewUniformGenerator(seed int64) *UniformGenerator

NewUniformGenerator returns a uniform-distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: urng := rng.NewUniformGenerator(time.Now().UnixNano())

func (UniformGenerator) Float32

func (ung UniformGenerator) Float32() float32

Float32 returns a random float32 in [0.0, 1.0)

func (UniformGenerator) Float32Range

func (ung UniformGenerator) Float32Range(a, b float32) float32

Float32Range returns a random float32 in [a, b)

func (UniformGenerator) Float32n

func (ung UniformGenerator) Float32n(n float32) float32

Float32n returns a random float32 in [0.0, n)

func (UniformGenerator) Float64

func (ung UniformGenerator) Float64() float64

Float64 returns a random float64 in [0.0, 1.0)

func (UniformGenerator) Float64Range

func (ung UniformGenerator) Float64Range(a, b float64) float64

Float32Range returns a random float32 in [a, b)

func (UniformGenerator) Float64n

func (ung UniformGenerator) Float64n(n float64) float64

Float64n returns a random float64 in [0.0, n)

func (UniformGenerator) Int32

func (ung UniformGenerator) Int32() int32

Int32 returns a random uint32

func (UniformGenerator) Int32Range

func (ung UniformGenerator) Int32Range(a, b int32) int32

Int32Range returns a random uint32 in [a, b)

func (UniformGenerator) Int32n

func (ung UniformGenerator) Int32n(n int32) int32

Int32n returns a random uint32 in [0, n)

func (UniformGenerator) Int64

func (ung UniformGenerator) Int64() int64

Int64 returns a random uint64

func (UniformGenerator) Int64Range

func (ung UniformGenerator) Int64Range(a, b int64) int64

Int64Range returns a random uint64 in [a, b)

func (UniformGenerator) Int64n

func (ung UniformGenerator) Int64n(n int64) int64

Int64n returns a random uint64 in [0, n)

func (UniformGenerator) Shuffle

func (ung UniformGenerator) Shuffle(arr []interface{})

Shuffle rearrange the elements of an array in random order

func (UniformGenerator) ShuffleRange

func (ung UniformGenerator) ShuffleRange(arr []interface{}, low, high int)

Shuffle rearrange the elements of the subarray[low..high] in random order

type WeibullGenerator

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

WeibullGenerator is a random number generator for weibull distribution. The zero value is invalid, use NewWeibullGenerator to create a generator

func NewWeibullGenerator

func NewWeibullGenerator(seed int64) *WeibullGenerator

NewWeibullGenerator returns a weibull-distribution generator it is recommended using time.Now().UnixNano() as the seed, for example: wrng := rng.NewWeibullGenerator(time.Now().UnixNano())

func (WeibullGenerator) Weibull

func (wrng WeibullGenerator) Weibull(lambda, k float64) float64

Weibull returns a random number of weibull distribution (lambda > 0.0 and k > 0.0)

Jump to

Keyboard shortcuts

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