rand

package module
v0.0.0-...-34764be Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2019 License: BSD-3-Clause Imports: 6 Imported by: 1

README

go-rand

GoDoc Build Status

A cryptographically secure pseudo-random number generator built from ChaCha20.

There are no security guarantees offered.

Benchmark

BenchmarkCryptoRand-8   	      20	  63222315 ns/op	  16.59 MB/s	[crypto/rand]
BenchmarkMathRand-8     	     500	   2572662 ns/op	 407.58 MB/s	[math/rand]
BenchmarkReader-8       	    2000	    777531 ns/op        1348.60 MB/s	[tmthrgd/go-rand - AVX only]

License

Unless otherwise noted, the go-rand source files are distributed under the Modified BSD License found in the LICENSE file.

Documentation

Overview

Package rand implements a cryptographically secure pseudorandom number generator.

Index

Examples

Constants

View Source
const (
	// SeedSize is the required length of seed passed
	// to New.
	SeedSize = chacha.KeySize
)

Variables

View Source
var (
	// Reader is a global, shared instance of a
	// cryptographically strong pseudo-random
	// generator.
	//
	// The seed is read from crypto/rand.Reader.
	Reader io.Reader = &seedOnFirstRead{}
)

Functions

func New

func New(seed []byte) (io.Reader, error)

New returns a new pseudorandom generator with the given seed. If seed == nil, the generator seeds itself by reading from crypto/rand.Reader. seed must be SeedSize bytes long.

The Read method on the returned reader always returns the full amount asked for, or else it returns an error.

The generator uses ChaCha20 reseeding after every 1 GB of generated data.

The generator is deterministic for a given seed.

func Read

func Read(b []byte) (n int, err error)

Read is a helper function that calls Reader.Read using io.ReadFull. On return, n == len(b) if and only if err == nil.

Example

This example reads 10 cryptographically secure pseudorandom numbers from rand.Reader and writes them to a byte slice.

c := 10
b := make([]byte, c)

_, err := Read(b)
if err != nil {
	fmt.Println("error:", err)
	return
}

// The slice should now contain random bytes instead of only zeroes.
fmt.Println(bytes.Equal(b, make([]byte, c)))
Output:

false

Types

This section is empty.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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