random

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2023 License: MIT Imports: 5 Imported by: 0

README

random

Build Issues Pull Requests Go Doc License

Overview

Produce randomness easily

Documentation

Overview

Package random provides a wrapper around some crypto/rand functions causing a panic rather than an error on failure to generate the random elements. The rationale is that we are not going to be able to continue should the secure random number generator fail, and we can't easily force a failure for testing. The package also provides some convenience functions for some basic random generation tasks, like secret creation.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Bytes

func Bytes(n int) []byte

Bytes returns securely generated random bytes. It will panic if the system's secure random number generator fails to function correctly.

Example
package main

import (
	"fmt"

	"bitbucket.org/idomdavis/random"
)

func main() {
	fmt.Println(len(random.Bytes(10)))

}
Output:

10

func Int

func Int(n int) int

Int returns a random integer such that 0 >= x < n.

Example
package main

import (
	"fmt"

	"bitbucket.org/idomdavis/random"
)

func main() {
	fmt.Println(random.Int(1) >= 0)
	fmt.Println(random.Int(10) > 10)

}
Output:

true
false

func MustInt

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

MustInt returns a random integer x, such that 0 >= x < n. MustInt will panic if the given reader fails.

func MustRead

func MustRead(n int, reader Reader) []byte

MustRead will read n bytes from the given reader, emitting a panic n < 1, if the reader returns an error or if we do not read n bytes.

func Secret

func Secret(n int) string

Secret returns a URL-safe, base64 encoded securely generated random string. The actual length of the string will be 1.333... bigger than n. It will panic if the system's secure random number generator fails to function correctly.

Example
package main

import (
	"fmt"

	"bitbucket.org/idomdavis/random"
)

func main() {
	fmt.Println(len(random.Secret(9)))

}
Output:

12

Types

type Reader

type Reader func([]byte) (int, error)

Reader function used by MustRead. This will usually be crypto/rand/Read.

Jump to

Keyboard shortcuts

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