iorand

package
v3.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2019 License: ISC Imports: 2 Imported by: 0

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

func New(r io.Reader, bo binary.ByteOrder) *IoRand

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:

func (*IoRand) Int63

func (r *IoRand) Int63() int64

Int63 returns a non-negative pseudo-random 63-bit integer as an int64.

func (*IoRand) Seed

func (r *IoRand) Seed(seed int64)

Seed is a no-op as IoRand does not support Seeding.

func (*IoRand) Uint64

func (r *IoRand) Uint64() (n uint64)

Uint64 returns a pseudo-random 64-bit value as a uint64. Returns 0 and sets r.Err to a non-nil value if an error occurs.

Jump to

Keyboard shortcuts

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