Documentation
¶
Overview ¶
Package iorand provides a ran64.Source64 wrapper around an io.Reader.
This implementation does not require seeding and can be used for example to read random numbers from crypto.rand.Reader.
Since there may be errors when reading from an io.Reader, special care must be taken when using this source. If an error occurs, IoRand.Uint64() returns 0 and IoRand.Err will be set to the latest error.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IoRand ¶
type IoRand struct { ByteOrder binary.ByteOrder Err error // latest io error // contains filtered or unexported fields }
IoRand is a wrapper around an io.Reader that implements the rand64.Source64 interface.
func New ¶
New returns a new IoRand wrapper around an io.Reader using the provided binary.ByteOrder for reads. Since IoRand will not buffer its input, providing a buffered Reader is recommended.
Example ¶
Wrap crypto/rand in an IoRand
package main import ( "bufio" crand "crypto/rand" "encoding/binary" "math/rand" "github.com/db47h/rand64/v3/iorand" ) func main() { // first, wrap crypto/rand.Reader in a buffered bufio.Reader bufferedReader := bufio.NewReader(crand.Reader) // Create the new IoRand ior := iorand.New(bufferedReader, binary.LittleEndian) // use it as rand.Source64 rng := rand.New(ior) // get random numbers... for i := 0; i < 4; i++ { _ = rng.Uint64() _ = rng.Int63() } }
Output: