random

package
v0.0.0-...-a9d0937 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: Apache-2.0 Imports: 6 Imported by: 9

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Duration

func Duration(generator SingleThreadedGenerator, maximum time.Duration) time.Duration

Duration that is randomly generated that lies between [0, maximum).

Types

type SingleThreadedGenerator

type SingleThreadedGenerator interface {
	// Generates a number in range [0.0, 1.0).
	Float64() float64
	// Generates a number in range [0, n), where n is of type int64.
	Int63n(n int64) int64
	// Generates a number in range [0, n), where n is of type int.
	Intn(n int) int
	// Generates arbitrary bytes of data. This method is guaranteed
	// to succeed.
	Read(p []byte) (int, error)
	// Shuffle the elements in a list.
	Shuffle(n int, swap func(i, j int))
	// Generates an arbitrary 64-bit integer value.
	Uint64() uint64
}

SingleThreadedGenerator is a Random Number Generator (RNG) that cannot be used concurrently. This interface is a subset of Go's rand.Rand.

func NewFastSingleThreadedGenerator

func NewFastSingleThreadedGenerator() SingleThreadedGenerator

NewFastSingleThreadedGenerator creates a new SingleThreadedGenerator that is not suitable for cryptographic purposes. The generator is randomly seeded.

type ThreadSafeGenerator

type ThreadSafeGenerator interface {
	SingleThreadedGenerator

	IsThreadSafe()
}

ThreadSafeGenerator is identical to SingleThreadedGenerator, except that it is safe to use from within multiple goroutines without additional locking. These generators may be slower than their single-threaded counterparts.

var CryptoThreadSafeGenerator ThreadSafeGenerator = cryptoThreadSafeGenerator{
	Rand: math_rand.New(cryptoSource64{}),
}

CryptoThreadSafeGenerator is an instance of ThreadSafeGenerator that is suitable for cryptographic purposes.

var FastThreadSafeGenerator ThreadSafeGenerator = &fastThreadSafeGenerator{
	generator: NewFastSingleThreadedGenerator(),
}

FastThreadSafeGenerator is an instance of ThreadSafeGenerator that is not suitable for cryptographic purposes. The generator is randomly seeded on startup.

Jump to

Keyboard shortcuts

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