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 ¶
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 ¶
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 ¶
MustInt returns a random integer x, such that 0 >= x < n. MustInt will panic if the given reader fails.
func MustRead ¶
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 ¶
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