Golang program to show use of rand package


In golang, a rand package is used to generate random numbers/characters. Firstly, it seeds with the current time to get the different random Output each time then perform any operation.

Syntax

rand.Seed(value)

Rand.Seed() function is used to generate random numbers. It takes a user input as argument which is the upper limit for generating random numbers.

func Now() Time

The Now() function is defined in time package. This function generates the current local time. To use this function, we have to first impot the time package in our program.

func (t Time) UnixNano() int64

The UnixNano() function is defined in time package. This function is used to get the number of seconds passed from January 1 1970 in utc. The final result returned by it is of integer type of 64 bits.

rand.Intn(n)

The Intn() function is defined in the math/rand package. It is used to generate a random number in the interval from [0, n]. where n is a number provided by user. The function throws an error if the provided number is lesser than 0.

func Strings(src []string) []string

The strings function is defined in sort package. This function takes the array in string format that we wish to sort and returns the result by sorting that array.

func make ([] type, size, capacity)

The make function in go language is used to create an array/map it accepts the type of variable to be created, its size and capacity as arguments.

func range(variable)

The range function is used to iterate over any data type. To use this, we first have to write the range keyword followed by the data type to which we want to iterate and as a result the loop will iterate till the last element of the variable.

Algorithm

  • Step 1 − Import the required packages in the program

  • Step 2 − Create a main function

  • Step 3 − In the main seed the random number generator with the current time to get different Output each time

  • Step 4 − Use the function from rand package to perform different operations

  • Step 5 − Print the Output on the console using Println function from the fmt package

Example 1

In this example, the random number generator will be seeded with the current time to get new random number. Then, in the first two cases random integer and float numbers will be generated then random permutation will be printed.

package main

import (
   "fmt"
   "math/rand"
   "time"
)


func main() {
   rand.Seed(time.Now().UnixNano())
   random_integer := rand.Intn(50)
   fmt.Println("Random integer:", random_integer)
   random_floatnum := rand.Float64()
   fmt.Println("Random float:", random_floatnum)  	
   random_permutation := rand.Perm(20)
   fmt.Println("Random permutation:", random_permutation) 
}

Output

Random integer: 0
Random float: 0.5038452930984003
Random permutation: [2 3 1 6 8 16 17 12 7 10 13 11 4 5 18 14 9 0 19 15]

Example 2

In this illustration, seeding will be done then a variable named password_value will be created using make function which is a built-in function in Golang to hold the random password generated from the charset.

package main

import (
   "fmt"
   "math/rand"
   "time"
)

const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" 

func main() {	 
   rand.Seed(time.Now().UnixNano())	
   password_value := make([]byte, 6)
   for i := range password_value {
      password_value[i] = charset[rand.Intn(len(charset))]  
   }
   fmt.Println("Random password:", string(password_value)) 
}

Output

Random password: 2sdGzJ

Conclusion

We compiled and executed the program of depicting the use of rand package using two examples. In the first example, we performed different operations by generating random integer, float and permutation. In the second example, a random password was generated to show use of rand package. Both the examples give desired random Output.

Updated on: 04-Apr-2023

193 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements