Open In App

rand package in Golang

Last Updated : 08 Jun, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Function Description
ExpFloat64 This function returns an exponentially distributed float64 in the range (0, +math.MaxFloat64] with an exponential distribution from the default source.
Float32 This function returns, as a float32, a pseudo-random number in [0.0, 1.0) from the default source.
Float64 This function returns, as a float64, a pseudo-random number in [0.0, 1.0) from the default source.
Int This function returns a non-negative pseudo-random int from the default source.
Int31 This function returns a non-negative pseudo-random 31-bit integer as an int32 from the default source.
Int31n This function returns, as an int32, a non-negative pseudo-random number in [0, n) from the default source.
Int63 This function returns a non-negative pseudo-random 63-bit integer as an int64 from the default source.
Int63n This function returns, as an int64, a non-negative pseudo-random number in [0, n) from the default source.
Intn This function returns, as an int, a non-negative pseudo-random number in [0, n) from the default source.
NormFloat64 This 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.
Perm This function returns, as a slice of n ints, a pseudo-random permutation of the integers [0, n) from the default source.
Read This function generates len(p) random bytes from the default source and writes them into p.
Seed This 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)..
Shuffle This function pseudo-randomizes the order of elements using the default source.
Uint32 This function returns a pseudo-random 32-bit value as a uint32 from the default source.
Uint64 This function returns a pseudo-random 64-bit value as a uint64 from the default source.

type Rand

Method Description
func New This function returns a new Rand that uses random values from src to generate other random values.
func (*Rand) ExpFloat64 This method is used to return an exponentially distributed float64 in the range (0, +math.MaxFloat64] with an exponential distribution.
func (*Rand) Float32 This method returns, as a float32, a pseudo-random number in [0.0, 1.0).
func (*Rand) Float64 This method returns, as a float64, a pseudo-random number in [0.0, 1.0).
func (*Rand) Int This method returns a non-negative pseudo-random int.
func (*Rand) Int31 This method returns a non-negative pseudo-random 31-bit integer as an int32.
func (*Rand) Int31n This method returns, as an int32, a non-negative pseudo-random number in [0, n).
func (*Rand) Int63 This method returns a non-negative pseudo-random 63-bit integer as an int64.
func (*Rand) Int63n This method returns, as an int64, a non-negative pseudo-random number in [0, n).
func (*Rand) Intn This method returns, as an int, a non-negative pseudo-random number in [0, n).
func (*Rand) NormFloat64 This 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) Perm This method returns, as a slice of n ints, a pseudo-random permutation of the integers [0, n).
func (*Rand) Read This method generates len(p) random bytes and writes them into p.
func (*Rand) Seed This method provided seed value to initialize the generator to a deterministic state.
func (*Rand) Shuffle This method pseudo-randomizes the order of elements.
func (*Rand) Uint32 This method returns a pseudo-random 32-bit value as a uint32.
func (*Rand) Uint64 This method returns a pseudo-random 64-bit value as a uint64.

type Source

Method Description
func NewSource This function returns a new pseudo-random Source seeded with the given value.
type Source64 It 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

Method Description
func NewZipf This function returns a Zipf variate generator.
func (*Zipf) Uint64 This 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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads