weyl

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2020 License: ISC Imports: 3 Imported by: 0

README

Middle Square Weyl Sequence RNG for Go

GoDoc

A new implementation of John von Neumann's middle square random number generator (RNG). A Weyl sequence is utilized to keep the generator running through a long period.

Paper

Using

To start using Weyl, install Go and run go get:

$ go get -u github.com/tidwall/weyl

Create a new RNG

rng := rand.New(weyl.NewSource(time.Now().UnixNano()))

Use the RNG just as you would use the standard math/rand.

rng.Int63()  // random int64
rng.Float64() // random float64

Performance

Weyl RNG:

BenchmarkInt63Threadsafe-8     	100000000	        16.3 ns/op
BenchmarkInt63Unthreadsafe-8   	300000000	         5.32 ns/op
BenchmarkIntn1000-8            	100000000	        16.0 ns/op
BenchmarkInt63n1000-8          	50000000	        25.7 ns/op
BenchmarkInt31n1000-8          	100000000	        14.6 ns/op
BenchmarkFloat32-8             	100000000	        11.9 ns/op
BenchmarkFloat64-8             	200000000	         8.44 ns/op
BenchmarkPerm3-8               	20000000	        78.0 ns/op
BenchmarkPerm30-8              	 2000000	       600 ns/op
BenchmarkRead3-8               	100000000	        14.8 ns/op
BenchmarkRead64-8              	10000000	       135 ns/op
BenchmarkRead1000-8            	 1000000	      1801 ns/op

Go standard math/rand:

BenchmarkInt63Threadsafe-8     	100000000	        21.4 ns/op
BenchmarkInt63Unthreadsafe-8   	200000000	         6.74 ns/op
BenchmarkIntn1000-8            	100000000	        17.0 ns/op
BenchmarkInt63n1000-8          	50000000	        26.4 ns/op
BenchmarkInt31n1000-8          	100000000	        14.4 ns/op
BenchmarkFloat32-8             	100000000	        12.9 ns/op
BenchmarkFloat64-8             	200000000	         9.77 ns/op
BenchmarkPerm3-8               	20000000	        80.6 ns/op
BenchmarkPerm30-8              	 2000000	       658 ns/op
BenchmarkRead3-8               	100000000	        14.6 ns/op
BenchmarkRead64-8              	10000000	       147 ns/op
BenchmarkRead1000-8            	 1000000	      2112 ns/op

Contact

Josh Baker @tidwall

License

Weyl source code is available under the ISC License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Int63

func Int63() int64

Int63 returns a random int64

func NewSource

func NewSource(seed int64) rand.Source

NewSource returns a new rand.Source for used with rand.New()

func NewSource64

func NewSource64(seed int64) rand.Source64

NewSource64 returns a new rand.Source64

Types

type Generator

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

Generator represents an random number generator

func (*Generator) Float64

func (w *Generator) Float64() float64

Float64 returns a random float64

func (*Generator) Int63

func (w *Generator) Int63() int64

Int63 returns a random Int63 returns a random int64 that is always a positive number.

func (*Generator) Seed

func (w *Generator) Seed(seed int64)

Seed or re-seed the generator

func (*Generator) Uint64

func (w *Generator) Uint64() uint64

Uint64 returns a random uint64

Jump to

Keyboard shortcuts

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