Documentation ¶
Overview ¶
This package provides an implementation of Go's math/rand API that is backed by the CSPRNG in crypto/rand. This allows using the nicer API from math/rand, but still ensuring that the random number source is cryptographically-secure.
If there is a case where this package cannot obtain random data from crypto/rand, it will panic. This is largely the solution you want, since this should only occur (on Linux) when the /dev/urandom pool hasn't been initialized yet. In this case, the calling code can't do anything sensible with the error anyway. Also, panicking on this error allows us to maintain the same API as math/rand.
Index ¶
- func ExpFloat64() float64
- func Float32() float32
- func Float64() float64
- func Int() int
- func Int31() int32
- func Int31n(n int32) int32
- func Int63() int64
- func Int63n(n int64) int64
- func Intn(n int) int
- func NormFloat64() float64
- func Perm(n int) []int
- func Read(p []byte) (n int, err error)
- func Seed(seed int64)
- func Shuffle(n int, swap func(i, j int))
- func Uint32() uint32
- func Uint64() uint64
- type CSRandSource
- type CSStatefulRandSource
- type Rand
- func (r Rand) ExpFloat64() float64
- func (r Rand) Float32() float32
- func (r Rand) Float64() float64
- func (r Rand) Int() int
- func (r Rand) Int31() int32
- func (r Rand) Int31n(n int32) int32
- func (r Rand) Int63() int64
- func (r Rand) Int63n(n int64) int64
- func (r Rand) Intn(n int) int
- func (r Rand) NormFloat64() float64
- func (r Rand) Perm(n int) []int
- func (r Rand) Read(p []byte) (n int, err error)
- func (r Rand) Seed(seed int64)
- func (r Rand) Shuffle(n int, swap func(i, j int))
- func (r Rand) Uint32() uint32
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ExpFloat64 ¶
func ExpFloat64() float64
func NormFloat64 ¶
func NormFloat64() float64
Types ¶
type CSRandSource ¶
type CSRandSource struct{}
CSRandSource is a math/rand.Source that wraps crypto/rand.Reader.
func (CSRandSource) Int63 ¶
func (r CSRandSource) Int63() int64
Int63 implements the math/rand.Source interface. Will panic if an error occurs when reading the underlying Reader.
func (CSRandSource) Seed ¶
func (r CSRandSource) Seed(s int64)
Seed implements the math/rand.Source interface. Does nothing.
func (CSRandSource) Uint64 ¶
func (r CSRandSource) Uint64() uint64
Uint64 implements the math/rand.Source64 interface. Will panic if an error occurs when reading the underlying Reader.
type CSStatefulRandSource ¶ added in v0.2.0
type CSStatefulRandSource struct {
// contains filtered or unexported fields
}
CSStatefulRandSource is a math/rand.Source that wraps crypto/rand.Reader that is not safe for use by concurrent goroutines. Similar to math/rand.Source, protect its usage by using a sync.Pool (recommended) or with a sync.Mutex.
func (*CSStatefulRandSource) Int63 ¶ added in v0.2.0
func (r *CSStatefulRandSource) Int63() int64
Int63 implements the math/rand.Source interface. Will panic if an error occurs when reading the underlying Reader.
func (*CSStatefulRandSource) Seed ¶ added in v0.2.0
func (r *CSStatefulRandSource) Seed(s int64)
Seed implements the math/rand.Source interface. Does nothing.
func (*CSStatefulRandSource) Uint64 ¶ added in v0.2.0
func (r *CSStatefulRandSource) Uint64() uint64
Uint64 implements the math/rand.Source64 interface. Will panic if an error occurs when reading the underlying Reader.
type Rand ¶
type Rand struct{}
A source of random numbers. Mimics the type of the same name in math/rand. This is useful if you want to use this package as a drop-in replacement for code that uses math/rand.
Note that since this package obtains all state from the crypto/rand.Reader, all methods of this struct use a single shared implementation.