rand

package module
v0.0.0-...-2f96c10 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2023 License: Apache-2.0 Imports: 3 Imported by: 0

README

rand

Go Reference godocs.io Go Report Card

Additional Sources for Go's math/rand

  • SplitMix64 - period 2^64
  • XOshiro256 - period 2^256 - 1
  • PCG XSL RR 128/64 (LCG) - period 2^128

Documentation

Overview

Package rand contains additional math/rand/Source64 implementations for use with math/rand's Rand type.

The standard math/rand Source64 pre v1.17 used an Additive Lagged Fibonacci Generator (ALFG) originally designed by DP Mitchell and JA Reeds for Bell Lab's Plan9 operating system. From 1.17 it uses the wyrand (runtime.fastrand) PRNG https://github.com/wangyi-fudan/wyhash

SplitMix64 (period 2^64)

A fixed-increment version of Java 8's SplittableRandom generator
See http://dx.doi.org/10.1145/2714064.2660195 and
http://docs.oracle.com/javase/8/docs/api/java/util/SplittableRandom.html
It is a very fast generator passing BigCrush, and it can be useful if
for some reason you absolutely want 64 bits of state; otherwise, we
rather suggest to use a xoroshiro128+ (for moderately parallel
computations) or xorshift1024* (for massively parallel computations)
generator.
- Sebastiano Vigna

XOshiro256 (xoshiro256** in the literature) (period 2^256 - 1)

According to Vigna, this is faster and produces better output than the xorshift family of generators.
(See https://en.wikipedia.org/wiki/Xorshift and https://prng.di.unimi.it/)

PCG (Permuted Congruential Generator) (period 2^128)

An implementation of the PCG XSL RR 128/64 (LCG) generator described in Melissa O'Neill's paper.
(See http://www.pcg-random.org/pdf/toms-oneill-pcg-family-v1.02.pdf)
There's an alternative implementation in golang.org/x/exp/rand.

Relative speeds (not very scientific)

Without/with locking
SplitMix64 0.345/1.540
XOshiro256 0.379/1.717
ALFG       0.429/1.748
PCG        0.454/1.728

The statistical testing suite Big Crush/TU01 is described at http://simul.iro.umontreal.ca/testu01/tu01.html and available at https://github.com/umontreal-simul/TestU01-2009/

Index

Constants

View Source
const (
	// MaxUint63 represents the largest value that can be held in a 63 bit unsigned integer.
	MaxUint63 = (1 << 63) - 1
)

Variables

This section is empty.

Functions

This section is empty.

Types

type LockableSource

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

LockableSource wraps a type implementing rand.Source64 in a mutex to make it goroutine safe.

func NewLockableSource

func NewLockableSource(src rand.Source64) *LockableSource

NewLockableSource returns src wrapped in a mutex.

func (*LockableSource) Int63

func (s *LockableSource) Int63() int64

Int63 implements the rand.Source interface.

func (*LockableSource) Seed

func (s *LockableSource) Seed(seed int64)

Seed implements the rand.Source interface.

func (*LockableSource) Uint64

func (s *LockableSource) Uint64() uint64

Uint64 implements the rand.Source64 interface.

type PCGSource

type PCGSource [2]uint64

PCGSource - size 128 bits, period 2^128.

func (*PCGSource) Int63

func (s *PCGSource) Int63() int64

Int63 implements the rand.Source interface.

func (*PCGSource) Seed

func (s *PCGSource) Seed(seed int64)

Seed implements the rand.Source interface.

func (*PCGSource) Uint64

func (s *PCGSource) Uint64() uint64

Uint64 implements the rand.Source64 interface.

type SplitMix64Source

type SplitMix64Source uint64

SplitMix64Source - size 64 bits, period 2^64.

func (*SplitMix64Source) Int63

func (s *SplitMix64Source) Int63() int64

Int63 implements the rand.Source interface.

func (*SplitMix64Source) Seed

func (s *SplitMix64Source) Seed(seed int64)

Seed implements the rand.Source interface.

func (*SplitMix64Source) Uint64

func (s *SplitMix64Source) Uint64() uint64

Uint64 implements the rand.Source64 interface.

type WySource

type WySource uint64

WySource (from https://github.com/wangyi-fudan/wyhash)

func (*WySource) Int63

func (s *WySource) Int63() int64

Int63 implements the rand.Source interface.

func (*WySource) Seed

func (s *WySource) Seed(seed int64)

Seed implements the rand.Source interface.

func (*WySource) Uint64

func (s *WySource) Uint64() uint64

Uint64 implements the rand.Source64 interface.

type XOshiro256Source

type XOshiro256Source [4]uint64

XOshiro256Source - size 256 bits, period 2^256 - 1. Do not leave zero initialized!

func (*XOshiro256Source) Int63

func (s *XOshiro256Source) Int63() int64

Int63 implements the rand.Source interface.

func (*XOshiro256Source) Jump

Jump is equivalent to 2^128 calls to Uint64(); it can be used to generate 2^128 non-overlapping subsequences for parallel computations.

func (*XOshiro256Source) LongJump

func (s *XOshiro256Source) LongJump() *XOshiro256Source

LongJump is equivalent to 2^192 calls to Uint64(); it can be used to generate 2^64 starting points, from each of which jump() will generate 2^64 non-overlapping subsequences for parallel distributed computations.

func (*XOshiro256Source) Seed

func (s *XOshiro256Source) Seed(seed int64)

Seed implements the rand.Source interface.

func (*XOshiro256Source) Uint64

func (s *XOshiro256Source) Uint64() uint64

Uint64 implements the rand.Source64 interface.

Jump to

Keyboard shortcuts

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