xoroshiro

package
v3.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2019 License: ISC Imports: 2 Imported by: 0

Documentation

Overview

Package xoroshiro provides an implementation for a pseudo-random number generator (PRNG) using the xoroshiro128** and xoroshiro128+ algorithms.

Period: 2^128-1. State size: 128 bits.

Go implementation based on a C reference implementation by David Blackman and Sebastiano Vigna. For further information: http://xoshiro.di.unimi.it/

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Rng128P

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

Rng128P encapsulates a xoroshiro128+ PRNG.

xoroshiro128+ 1.0 is Blackman & Vigna's best and fastest small-state generator for floating-point numbers. They suggest to use its upper bits for floating-point generation, as it is slightly faster than xoroshiro128**. It passes all tests the authors are aware of except for the four lower bits, which might fail linearity tests (and just those), so if low linear complexity is not considered an issue (as it is usually the case) it can be used to generate 64-bit outputs, too; moreover, this generator has a very mild Hamming-weight dependency making our test (http://prng.di.unimi.it/hwd.php) fail after 5 TB of output; the authors believe this slight bias cannot affect any application. If you are concerned, use xoroshiro128** or xoshiro256+.

The authors suggest to use a sign test to extract a random Boolean value, and right shifts to extract subsets of bits.

Note that the Go implementation of Rand.Float64 uses the upper bits as suggested.

Example
package main

import (
	"fmt"
	"math/rand"

	"github.com/db47h/rand64/v3/xoroshiro"
)

const SEED1 = 1387366483214

func main() {
	src := xoroshiro.Rng128P{}
	src.Seed(SEED1)
	rng := rand.New(&src)
	for i := 0; i < 4; i++ {
		fmt.Printf(" %d", rng.Uint32())
	}
	fmt.Println("")
	for i := 0; i < 4; i++ {
		fmt.Printf(" %d", rng.Uint64())
	}
	fmt.Println("")
	// Play craps
	for i := 0; i < 10; i++ {
		fmt.Printf(" %d%d", rng.Intn(6)+1, rng.Intn(6)+1)
	}

}
Output:

3672052799 776653214 1122818236 1139848352
 14850484681238877506 7018105211938886447 5908230704518956940 2042158984393296588
 65 53 21 56 44 16 23 42 55 41

func (*Rng128P) Int63

func (rng *Rng128P) Int63() int64

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

func (*Rng128P) Seed

func (rng *Rng128P) Seed(seed int64)

Seed uses the provided seed value to initialize the generator to a deterministic state.

func (*Rng128P) Uint64

func (rng *Rng128P) Uint64() uint64

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

type Rng128SS

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

Rng128SS encapsulates a xoroshiro128** PRNG.

xoroshiro128** 1.0 is Blackman & Vigna's all-purpose, rock-solid, small-state generator. It is extremely (sub-ns) fast and it passes all tests the authors are aware of, but its state space is large enough only for mild parallelism.

For generating just floating-point numbers, xoroshiro128+ is even faster (but it has a very mild bias, see notes in the comments).

Example
package main

import (
	"fmt"
	"math/rand"

	"github.com/db47h/rand64/v3/xoroshiro"
)

const SEED1 = 1387366483214

func main() {
	src := xoroshiro.Rng128SS{}
	src.Seed(SEED1)
	rng := rand.New(&src)
	for i := 0; i < 4; i++ {
		fmt.Printf(" %d", rng.Uint32())
	}
	fmt.Println("")
	for i := 0; i < 4; i++ {
		fmt.Printf(" %d", rng.Uint64())
	}
	fmt.Println("")
	// Play craps
	for i := 0; i < 10; i++ {
		fmt.Printf(" %d%d", rng.Intn(6)+1, rng.Intn(6)+1)
	}

}
Output:

901646676 398979522 1208087553 1093404254
 17905646702528074117 5693647338227160345 1089260090730707711 12276528025967720504
 41 35 56 61 56 35 31 12 63 54

func (*Rng128SS) Int63

func (rng *Rng128SS) Int63() int64

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

func (*Rng128SS) Seed

func (rng *Rng128SS) Seed(seed int64)

Seed uses the provided seed value to initialize the generator to a deterministic state.

func (*Rng128SS) Uint64

func (rng *Rng128SS) Uint64() uint64

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

Jump to

Keyboard shortcuts

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