xrand

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2023 License: MIT Imports: 11 Imported by: 1

Documentation

Overview

Package xrand provides helper functions for generating random numbers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes(size int) []byte

Bytes returns an amount of cryptographically-secure random bytes equal to the the specified size.

The bytes are generated using the crypto/rand package and will panic if crypto/rand fails to generate the requested amount of bytes.

Defaults to 32 if no size is specified.

func BytesWithReader

func BytesWithReader(size int, reader io.Reader) []byte

BytesWithReader is like Bytes, but allows you to specify your own reader.

func Int63

func Int63() int64

Int63 returns a non-negative cryptographically-secure random 63-bit integer as an int64. It panics if crypto/rand fails to generate random bytes.

func IntChaChaCha

func IntChaChaCha(n int, reader io.Reader) int

IntChaChaCha returns a cryptographically secure random integer in the range [0, n) using the ChaCha20 stream cipher. If the provided io.Reader is nil, it defaults to using crypto/rand.Reader.

func NewSource64

func NewSource64() (mrand.Source64, error)

NewSource64 returns a new cryptographically secure random number generator that satisfies the math/rand.Source64 interface.

func Shuffle

func Shuffle(str []string, reader io.Reader)

Shuffle randomizes the order of elements inside a string slice using the Fisher-Yates shuffle algorithm and a ChaCha20-based random number generator.

Types

type Source64

type Source64 struct {
	// Stream is the underlying AES-256 CTR stream.
	cipher.Stream
	// contains filtered or unexported fields
}

Source64 implements the math/rand.Source64 interface as a cryptographically secure random number generator using AES-256 in CTR mode.

func (*Source64) Int63

func (s *Source64) Int63() int64

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

func (*Source64) Seed

func (*Source64) Seed(_ int64)

Seed is a no-op that panics.

func (*Source64) Uint64

func (s *Source64) Uint64() uint64

Uint64 returns a random uint64.

Jump to

Keyboard shortcuts

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