random

package
v0.0.0-...-ff8c80c Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: Apache-2.0 Imports: 5 Imported by: 11

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.
	Int64N(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 32-bit integer value.
	Uint32() uint32
	// 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(cryptoSource{}),
}

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

var FastThreadSafeGenerator ThreadSafeGenerator = fastThreadSafeGenerator{}

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