Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

rand package in Golang

  • Last Updated : 08 Jun, 2020

Go language provides inbuilt support for generating random numbers of the specified type with the help of a math/rand package. This package implements pseudo-random number generators. These random numbers are generated by a source and this source produces a deterministic sequence of values every time when the program run. And if you want to random numbers for security-sensitive work, then use the crypto/rand package.

Note: In this package, the mathematical interval notation such as [0, n) is used.

FunctionDescription
ExpFloat64This function returns an exponentially distributed float64 in the range (0, +math.MaxFloat64] with an exponential distribution from the default source.
Float32This function returns, as a float32, a pseudo-random number in [0.0, 1.0) from the default source.
Float64This function returns, as a float64, a pseudo-random number in [0.0, 1.0) from the default source.
IntThis function returns a non-negative pseudo-random int from the default source.
Int31This function returns a non-negative pseudo-random 31-bit integer as an int32 from the default source.
Int31nThis function returns, as an int32, a non-negative pseudo-random number in [0, n) from the default source.
Int63This function returns a non-negative pseudo-random 63-bit integer as an int64 from the default source.
Int63nThis function returns, as an int64, a non-negative pseudo-random number in [0, n) from the default source.
IntnThis function returns, as an int, a non-negative pseudo-random number in [0, n) from the default source.
NormFloat64This function returns a normally distributed float64 in the range [-math.MaxFloat64, +math.MaxFloat64] with standard normal distribution (mean = 0, stddev = 1) from the default source.
PermThis function returns, as a slice of n ints, a pseudo-random permutation of the integers [0, n) from the default source.
ReadThis function generates len(p) random bytes from the default source and writes them into p.
SeedThis function provided seed value to initialize the default source to a deterministic state and if Seed is not called, the generator behaves as if seeded by Seed(1)..
ShuffleThis function pseudo-randomizes the order of elements using the default source.
Uint32This function returns a pseudo-random 32-bit value as a uint32 from the default source.
Uint64This function returns a pseudo-random 64-bit value as a uint64 from the default source.

type Rand

MethodDescription
func NewThis function returns a new Rand that uses random values from src to generate other random values.
func (*Rand) ExpFloat64This method is used to return an exponentially distributed float64 in the range (0, +math.MaxFloat64] with an exponential distribution.
func (*Rand) Float32This method returns, as a float32, a pseudo-random number in [0.0, 1.0).
func (*Rand) Float64This method returns, as a float64, a pseudo-random number in [0.0, 1.0).
func (*Rand) IntThis method returns a non-negative pseudo-random int.
func (*Rand) Int31This method returns a non-negative pseudo-random 31-bit integer as an int32.
func (*Rand) Int31nThis method returns, as an int32, a non-negative pseudo-random number in [0, n).
func (*Rand) Int63This method returns a non-negative pseudo-random 63-bit integer as an int64.
func (*Rand) Int63nThis method returns, as an int64, a non-negative pseudo-random number in [0, n).
func (*Rand) IntnThis method returns, as an int, a non-negative pseudo-random number in [0, n).
func (*Rand) NormFloat64This method is used to return a normally distributed float64 in the range -math.MaxFloat64 through +math.MaxFloat64 inclusive, with standard normal distribution (mean = 0, stddev = 1).
func (*Rand) PermThis method returns, as a slice of n ints, a pseudo-random permutation of the integers [0, n).
func (*Rand) ReadThis method generates len(p) random bytes and writes them into p.
func (*Rand) SeedThis method provided seed value to initialize the generator to a deterministic state.
func (*Rand) ShuffleThis method pseudo-randomizes the order of elements.
func (*Rand) Uint32This method returns a pseudo-random 32-bit value as a uint32.
func (*Rand) Uint64This method returns a pseudo-random 64-bit value as a uint64.

type Source

MethodDescription
func NewSourceThis function returns a new pseudo-random Source seeded with the given value.
type Source64It is a Source that can also generate uniformly-distributed pseudo-random uint64 values in the range [0, 1<<64) directly. If a Rand r's underlying Source s implements Source64, then r.Uint64 returns the result of one call to s.Uint64 instead of making two calls to s.Int63.

type Zipf



MethodDescription
func NewZipfThis function returns a Zipf variate generator.
func (*Zipf) Uint64This method returns a value drawn from the Zipf distribution described by the Zipf object.

Example 1:




// Golang program to illustrate 
// how to Get Intn Type Random  
// Number 
package main 
     
import ( 
    "fmt"
    "math/rand"
     
// Main function 
func main() { 
     
    // Finding random numbers of int type 
    // Using Intn() function 
    res_1 := rand.Intn(7) 
    res_2 := rand.Intn(8) 
    res_3 := rand.Intn(2) 
     
    // Displaying the result 
    fmt.Println("Random Number 1: ", res_1) 
    fmt.Println("Random Number 2: ", res_2) 
    fmt.Println("Random Number 3: ", res_3) 

Output:

Random Number 1:  6
Random Number 2:  7
Random Number 3:  1

Example 2:




// Golang program to illustrate 
// how to get a random number 
package main 
   
import ( 
    "fmt"
    "math/rand"
   
// Main function 
func main() { 
   
    // Finding random numbers of int type 
    // Using Int31() function 
    res_1 := rand.Int31() 
    res_2 := rand.Int31() 
    res_3 := rand.Int31() 
   
    // Displaying the result 
    fmt.Println("Random Number 1: ", res_1) 
    fmt.Println("Random Number 2: ", res_2) 
    fmt.Println("Random Number 3: ", res_3) 

Output:

Random Number 1:  1298498081
Random Number 2:  2019727887
Random Number 3:  1427131847



My Personal Notes arrow_drop_up
Recommended Articles
Page :