randtool

package
v0.0.53 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 10 Imported by: 0

README

randtool#

randtool is a thread safe based on rand. It was originally implemented in golang by gosl. Due to lightweight requirements, so only copy the some rnd function from gosl.

Install

go get -u -v github.com/adimax2953/go-tool/randtool

Usage

Let's start with a trivial example:

package main

import (
	"github.com/adimax2953/go-tool/randtool"
	"fmt"
)

func main() {
    r := randtool.New(rand.NewSource(4872))
	r.Seed(4872)

	println("When seed is 4872. Int63r(0,10) return", r.Int63r(0, 10), ".")
}

Benckmark

go test -benchmem -bench .
goos: windows
goarch: amd64
pkg: github.com/adimax2953/go-tool/randtool
cpu: 12th Gen Intel(R) Core(TM) i7-1260P
Benchmark_rand_Int63Threadsafe-16               99378988                12.57 ns/op            0 B/op          0 allocs/op
Benchmark_mt_Int63Threadsafe-16                 85725919                12.91 ns/op            0 B/op          0 allocs/op
Benchmark_rand_Int63ThreadsafeParallel-16       19362708                61.10 ns/op            0 B/op          0 allocs/op
Benchmark_rand_Int63Unthreadsafe-16             85503579                12.70 ns/op            0 B/op          0 allocs/op
Benchmark_mt_Int63ThreadsafeParallel-16         16552088                60.95 ns/op            0 B/op          0 allocs/op
Benchmark_rand_New-16                             136260              8189 ns/op               0 B/op          0 allocs/op
Benchmark_mt_New-16                             55566102                18.68 ns/op            0 B/op          0 allocs/op
Benchmark_rand_Intn1000-16                      80036816                14.03 ns/op            0 B/op          0 allocs/op
Benchmark_mt_Intn1000-16                        74885331                13.67 ns/op            0 B/op          0 allocs/op
Benchmark_rand_Int63n1000-16                    85495660                12.51 ns/op            0 B/op          0 allocs/op
Benchmark_mt_Int63n1000-16                      74875051                13.56 ns/op            0 B/op          0 allocs/op
Benchmark_rand_Int31n1000-16                    85672672                12.32 ns/op            0 B/op          0 allocs/op
Benchmark_mt_Int31n1000-16                      74610160                13.70 ns/op            0 B/op          0 allocs/op
Benchmark_rand_Float32-16                       105455782               12.10 ns/op            0 B/op          0 allocs/op
Benchmark_mt_Float32-16                         79111315                12.67 ns/op            0 B/op          0 allocs/op
Benchmark_rand_Float64-16                       85725919                11.94 ns/op            0 B/op          0 allocs/op
Benchmark_mt_Float64-16                         93917164                12.21 ns/op            0 B/op          0 allocs/op
Benchmark_rand_Perm3-16                         35260750                32.91 ns/op           24 B/op          1 allocs/op
Benchmark_mt_Perm3-16                           27004096                38.24 ns/op           24 B/op          1 allocs/op
Benchmark_rand_Perm30-16                         6019870               198.9 ns/op           240 B/op          1 allocs/op
Benchmark_mt_Perm30-16                           4984977               230.4 ns/op           240 B/op          1 allocs/op
Benchmark_rand_Perm30ViaShuffle-16              10000416               119.4 ns/op             0 B/op          0 allocs/op
Benchmark_mt_Perm30ViaShuffle-16                 6696027               165.1 ns/op             0 B/op          0 allocs/op
Benchmark_rand_ShuffleOverhead-16                6019665               197.8 ns/op             0 B/op          0 allocs/op
Benchmark_mt_ShuffleOverhead-16                  4278529               274.1 ns/op             0 B/op          0 allocs/op
Benchmark_rand_Read3-16                         74887200                13.54 ns/op            0 B/op          0 allocs/op
Benchmark_mt_Read3-16                           74952217                14.10 ns/op            0 B/op          0 allocs/op
Benchmark_rand_Read64-16                        24815178                42.52 ns/op            0 B/op          0 allocs/op
Benchmark_mt_Read64-16                          18323883                61.53 ns/op            0 B/op          0 allocs/op
Benchmark_rand_Read1000-16                       2666455               474.4 ns/op             0 B/op          0 allocs/op
Benchmark_mt_Read1000-16                         1274896               874.1 ns/op             0 B/op          0 allocs/op
Benchmark_rand_Int63r1000-16                    85711836                12.49 ns/op            0 B/op          0 allocs/op
Benchmark_mt_Int63r1000-16                      100971969               13.01 ns/op            0 B/op          0 allocs/op
Benchmark_rand_Int63s30-16                       3398407               344.7 ns/op             0 B/op          0 allocs/op
Benchmark_mt_Int63s30-16                         2921733               409.5 ns/op             0 B/op          0 allocs/op
ok      github.com/adimax2953/go-tool/randtool  112.697s




TODO

  1. Add TG Us test method.
  2. Improve or remove useless code.
  3. Check code formatting.

Documentation

Index

Constants

View Source
const (
	NMaxHit     = 5
	NEnlarge    = 30000
	BaseEnlarge = 100
)

Variables

This section is empty.

Functions

func ConvertRTPFix added in v0.0.9

func ConvertRTPFix(value []int64) [NMaxHit]int64

func GenRandArray

func GenRandArray(weightArray []int32, arraySizze int32) uint32

GenRandArray - safe

func GetRandom

func GetRandom(maxN int32) int32

GetRandom - safe

func Lottery added in v0.0.9

func Lottery[T NonNegative_Integer](values []T) int

Lottery - 長度4的陣列

func OpenPoint added in v0.0.9

func OpenPoint(calcWeight [NMaxHit][2]int64) int

func Shuffle

func Shuffle[T NonNegative_Integer](nums []T) []T

Shuffle -打亂陣列

func Uint32

func Uint32() uint32

Uint32 - returns pseudorandom uint32.

It is safe calling this function from concurrent goroutines.

func Uint32n

func Uint32n(maxN uint32) uint32

Uint32n - safe

Types

type Negative_Number added in v0.0.17

type Negative_Number interface {
	~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64
}

type NonNegative_Integer added in v0.0.17

type NonNegative_Integer interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64
}

type NonNegative_Number added in v0.0.17

type NonNegative_Number interface {
	~float32 | ~float64 | ~complex64 | ~complex128
}

type RNG

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

RNG is a pseudorandom number generator.

It is unsafe to call RNG methods from concurrent goroutines.

func (*RNG) GenRandArray

func (r *RNG) GenRandArray(weightArray []int32, arraySizze int32) uint32

GenRandArray -

func (*RNG) GetRandom

func (r *RNG) GetRandom(maxN int32) int32

GetRandom -

func (*RNG) Uint32

func (r *RNG) Uint32() uint32

Uint32 returns pseudorandom uint32.

It is unsafe to call this method from concurrent goroutines.

func (*RNG) Uint32n

func (r *RNG) Uint32n(maxN uint32) uint32

Uint32n returns pseudorandom uint32 in the range [0..maxN).

It is unsafe to call this method from concurrent goroutines.

type Rand

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

func New

func New(source rand.Source) *Rand

New returns a new gosfmt Rand that uses random values from src to generate other random values.

func (*Rand) ExpFloat64

func (r *Rand) ExpFloat64() float64

ExpFloat64 returns an exponentially distributed float64 in the range (0, +math.MaxFloat64] with an exponential distribution whose rate parameter (lambda) is 1 and whose mean is 1/lambda (1) from the default Source. To produce a distribution with a different rate parameter, callers can adjust the output using:

sample = ExpFloat64() / desiredRateParameter

func (*Rand) FlipCoin

func (r *Rand) FlipCoin(p float64) bool

FlipCoin generates a Bernoulli variable; throw a coin with probability p

func (*Rand) Float32

func (r *Rand) Float32() float32

Float32 returns, as a float32, a pseudo-random number in [0.0,1.0).

func (*Rand) Float32Shuffle

func (r *Rand) Float32Shuffle(values []float32)

Float32Shuffle shuffles a slice of float point numbers

func (*Rand) Float32r

func (r *Rand) Float32r(low, high float32) float32

Float32r generates a pseudo random real number between low and high; i.e. in [low, right)

Input:
 low  -- lower limit (closed)
 high -- upper limit (open)
Output:
 random float32

func (*Rand) Float32s

func (r *Rand) Float32s(values []float32, low, high float32)

Float32s generates pseudo random real numbers between low and high; i.e. in [low, right)

Input:
 low  -- lower limit (closed)
 high -- upper limit (open)
Output:
 values -- slice to be filled with len(values) numbers

func (*Rand) Float64

func (r *Rand) Float64() float64

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

func (*Rand) Float64Shuffle

func (r *Rand) Float64Shuffle(values []float64)

Float64Shuffle shuffles a slice of float point numbers

func (*Rand) Float64r

func (r *Rand) Float64r(low, high float64) float64

Float64r generates a pseudo random real number between low and high; i.e. in [low, right)

Input:
 low  -- lower limit (closed)
 high -- upper limit (open)
Output:
 random float64

func (*Rand) Float64s

func (r *Rand) Float64s(values []float64, low, high float64)

Float64s generates pseudo random real numbers between low and high; i.e. in [low, right)

Input:
 low  -- lower limit (closed)
 high -- upper limit (open)
Output:
 values -- slice to be filled with len(values) numbers

func (*Rand) Int

func (r *Rand) Int() int

Int returns a non-negative pseudo-random int.

func (*Rand) Int31

func (r *Rand) Int31() int32

Int31 returns a non-negative pseudo-random 31-bit integer as an int32.

func (*Rand) Int31Shuffle

func (r *Rand) Int31Shuffle(values []int32)

Int31Shuffle - shuffles a slice of integers

func (*Rand) Int31n

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

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

func (*Rand) Int31r

func (r *Rand) Int31r(low, high int32) int32

Int31r is int range generates pseudo random int32 between low and high.

Input:
 low  -- lower limit
 high -- upper limit
Output:
 random int32

func (*Rand) Int31s

func (r *Rand) Int31s(values []int32, low, high int32)

Int31s generates pseudo random integers between low and high.

Input:
 low    -- lower limit
 high   -- upper limit
Output:
 values -- slice to be filled with len(values) numbers

func (*Rand) Int63

func (r *Rand) Int63() int64

Int63 returns a non-negative pseudo-random 63-bit integer as an int64.

func (*Rand) Int63Shuffle

func (r *Rand) Int63Shuffle(values []int64)

Int63Shuffle - shuffles a slice of integers

func (*Rand) Int63n

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

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

func (*Rand) Int63r

func (r *Rand) Int63r(low, high int64) int64

Int63r generates pseudo random int64 between low and high.

Input:
 low  -- lower limit
 high -- upper limit
Output:
 random int64

func (*Rand) Int63s

func (r *Rand) Int63s(values []int64, low, high int64)

Int63s generates pseudo random integers between low and high.

Input:
 low    -- lower limit
 high   -- upper limit
Output:
 values -- slice to be filled with len(values) numbers

func (*Rand) IntShuffle

func (r *Rand) IntShuffle(values []int)

IntShuffle shuffles a slice of integers

func (*Rand) Intn

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

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

func (*Rand) Intr

func (r *Rand) Intr(low, high int) int

Intr is int range generates pseudo random integer between low and high.

Input:
 low  -- lower limit
 high -- upper limit
Output:
 random integer

func (*Rand) Ints

func (r *Rand) Ints(values []int, low, high int)

Ints generates pseudo random integers between low and high.

Input:
 low    -- lower limit
 high   -- upper limit
Output:
 values -- slice to be filled with len(values) numbers

func (*Rand) NormFloat64

func (r *Rand) NormFloat64() float64

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

sample = NormFloat64() * desiredStdDev + desiredMean

func (*Rand) Perm

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

Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n).

func (*Rand) Read

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

Read generates len(p) random bytes and writes them into p. It always returns len(p) and a nil error. Read should not be called concurrently with any other Rand method.

func (*Rand) Seed

func (r *Rand) Seed(seed int64)

Seed uses the provided seed value to initialize the generator to a deterministic state. Seed should not be called concurrently with any other Rand method.

func (*Rand) Shuffle

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

Shuffle pseudo-randomizes the order of elements. n is the number of elements. Shuffle panics if n < 0. swap swaps the elements with indexes i and j.

func (*Rand) Uint32

func (r *Rand) Uint32() uint32

Uint32 returns a pseudo-random 32-bit value as a uint32.

func (*Rand) Uint32Shuffle

func (r *Rand) Uint32Shuffle(values []uint32)

Uint32Shuffle shuffles a slice of integers

func (*Rand) Uint32r

func (r *Rand) Uint32r(low, high uint32) uint32

Uint32 is int range generates pseudo random uint32 between low and high.

Input:
 low  -- lower limit
 high -- upper limit
Output:
 random uint32

func (*Rand) Uint32s

func (r *Rand) Uint32s(values []uint32, low, high uint32)

Uint32s generates pseudo random integers between low and high.

Input:
 low    -- lower limit
 high   -- upper limit
Output:
 values -- slice to be filled with len(values) numbers

func (*Rand) Uint64

func (r *Rand) Uint64() uint64

Uint64 returns a pseudo-random 64-bit value as a uint64.

func (*Rand) Uint64Shuffle

func (r *Rand) Uint64Shuffle(values []uint64)

Uint64Shuffle - shuffles a slice of integers

func (*Rand) Uint64r

func (r *Rand) Uint64r(low, high uint64) uint64

Uint64r generates pseudo random uint64 between low and high.

Input:
 low  -- lower limit
 high -- upper limit
Output:
 random uint64

func (*Rand) Uint64s

func (r *Rand) Uint64s(values []uint64, low, high uint64)

Uint64s generates pseudo random integers between low and high.

Input:
 low    -- lower limit
 high   -- upper limit
Output:
 values -- slice to be filled with len(values) numbers

Jump to

Keyboard shortcuts

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