How to Get Int Type Random Number in Golang?


Generating random numbers is a common requirement in many programming applications. Golang offers built-in functionality to generate random numbers of different types. In this article, we will discuss how to generate random Int type numbers in Golang.

What is Int Type?

Int is a data type in Golang that represents an integer number. The size of the Int type depends on the architecture of the computer and can be either 32 or 64 bits. Int types are commonly used in programming applications for counting and indexing.

Generating Random Int Type Numbers in Golang

To generate a random Int type number in Golang, we can use the rand.Intn() function from the math/rand package. This function returns a random integer in the range [0, n), where n is the argument passed to the function.

Example

Here's an example code snippet −

package main

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

func main() {
   // Set the seed value for the random number generator
   rand.Seed(time.Now().UnixNano())

   // Generate a random Int type number between 0 and 9
   randNum := rand.Intn(10)

   // Print the random number
   fmt.Println(randNum)
}

In this example, we first set the seed value for the random number generator using the rand.Seed() function. This ensures that the sequence of random numbers generated is deterministic and reproducible.

Output

7

To generate a different random number each time you run the program, you can use the current timestamp as the seed value −

rand.Seed(time.Now().UnixNano())

Next, we use the rand.Intn() function to generate a random Int type number between 0 and 9 (inclusive) and assign it to the randNum variable. Finally, we print the random number using the fmt.Println() function.

Changing the Range of Generated Int Type Numbers

If we want to generate a random Int type number within a specific range, we can use the following formula −

Syntax

rand.Intn(max - min + 1) + min

where min and max are the minimum and maximum values of the range, respectively.

Example

Here's an example code snippet −

package main

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

func main() {
   // Set the seed value for the random number generator
   rand.Seed(time.Now().UnixNano())

   // Generate a random Int type number between 1 and 10
   randNum := rand.Intn(10-1+1) + 1

   // Print the random number
   fmt.Println(randNum)
}

In this example, we generate a random Int type number between 1 and 10 by using the formula rand.Intn(10-1+1) + 1. The result is assigned to the randNum variable and printed using the fmt.Println() function.

Output

3

Conclusion

Generating random Int type numbers in Golang is easy and straightforward using the rand.Intn() function from the math/rand package. By setting the seed value for the random number generator and using the appropriate formula, we can generate random numbers within a specific range. With this knowledge, you can now incorporate random numbers into your Golang applications.

Updated on: 05-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements