rand

package module
v0.0.0-...-2f07823 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2021 License: Apache-2.0 Imports: 5 Imported by: 30

README

golang rand package

GoPkg Widget

Package rand provides random number/bytes related functions.

The Rand type is similar to math/rand.Rand, except that it prefers to use crypto/rand to implement functions.

NOTE

Because we use crypto/rand to implement functions, the performance is not as good as math/rand. If you care about performance, please use it carefully.

Documentation

Overview

Package rand provides random number/bytes related functions.

The Rand type is similar to math/rand.Rand, except that it prefers to use crypto/rand to implement functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Float32

func Float32() float32

Float32 returns, as a float32, a random number in the half-open interval [0.0,1.0).

func Float64

func Float64() float64

Float64 returns, as a float64, a random number in the half-open interval [0.0,1.0).

func Int

func Int() int

Int returns a non-negative random int.

func Int31

func Int31() int32

Int31 returns a non-negative random 31-bit integer as an int32.

func Int31n

func Int31n(n int32) int32

Int31n returns, as an int32, a non-negative random number in the half-open interval [0,n). It panics if n <= 0.

func Int63

func Int63() int64

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

func Int63n

func Int63n(n int64) int64

Int63n returns, as an int64, a non-negative random number in the half-open interval [0,n). It panics if n <= 0.

func Intn

func Intn(n int) int

Intn returns, as an int, a non-negative random number in the half-open interval [0,n). It panics if n <= 0.

func New deprecated

func New() (result [16]byte)

New returns 16 random bytes.

The result is not printable, you can use encoding/hex or encoding/base64 to print it.

Deprecated: Please use the Read function directly.

func NewHex deprecated

func NewHex() (result []byte)

NewHex returns 32 hex-encoded random bytes.

Deprecated: Please use the Read function directly.

func Read

func Read(p []byte) (n int, err error)

Read generates len(p) random bytes and writes them into p. It always returns len(p) and a nil error.

func Uint

func Uint() uint

Uint returns a random uint.

func Uint32

func Uint32() uint32

Uint32 returns a random 32-bit value as a uint32.

func Uint64

func Uint64() uint64

Uint64 returns a random 64-bit value as a uint64.

Types

type Rand

type Rand struct {
	// contains filtered or unexported fields
}

Rand is similar to "math/rand.Rand", but it prefers "crypto/rand" to generate random bytes.

unlike "math/rand.Rand", all methods of *Rand are safe for concurrent use by multiple goroutines.

func NewRand

func NewRand() *Rand

NewRand returns a new Rand.

func (*Rand) Float32

func (r *Rand) Float32() float32

Float32 returns, as a float32, a random number in the half-open interval [0.0,1.0).

func (*Rand) Float64

func (r *Rand) Float64() float64

Float64 returns, as a float64, a random number in the half-open interval [0.0,1.0).

func (*Rand) Int

func (r *Rand) Int() int

Int returns a non-negative random int.

func (*Rand) Int31

func (r *Rand) Int31() int32

Int31 returns a non-negative random 31-bit integer as an int32.

func (*Rand) Int31n

func (r *Rand) Int31n(n int32) int32

Int31n returns, as an int32, a non-negative random number in the half-open interval [0,n). It panics if n <= 0.

func (*Rand) Int63

func (r *Rand) Int63() int64

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

func (*Rand) Int63n

func (r *Rand) Int63n(n int64) int64

Int63n returns, as an int64, a non-negative random number in the half-open interval [0,n). It panics if n <= 0.

func (*Rand) Intn

func (r *Rand) Intn(n int) int

Intn returns, as an int, a non-negative random number in the half-open interval [0,n). It panics if n <= 0.

func (*Rand) Read

func (r *Rand) Read(p []byte) (n int, err error)

Read generates len(p) random bytes and writes them into p. It always returns len(p) and a nil error.

func (*Rand) Uint

func (r *Rand) Uint() uint

Uint returns a random uint.

func (*Rand) Uint32

func (r *Rand) Uint32() uint32

Uint32 returns a random 32-bit value as a uint32.

func (*Rand) Uint64

func (r *Rand) Uint64() uint64

Uint64 returns a random 64-bit value as a uint64.

Jump to

Keyboard shortcuts

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