How to Get Int31 Type Random Number in Golang?


Random number generation is an essential feature of many programming applications. In Golang, you can generate random numbers of different types, including Int31. In this article, we will discuss how to generate random Int31 type numbers in Golang.

What is Int31 Type?

Int31 is a data type in Golang that represents a signed 32-bit integer number. The Int31 type is useful when you need to generate random numbers within the range of -2147483648 to 2147483647.

Generating Random Int31 Type Numbers in Golang

To generate a random Int31 type number in Golang, you can use the rand.Int31() function from the math/rand package. This function returns a random 31-bit signed integer.

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())

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 Int31 type number
   randNum := rand.Int31()

   // 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

181769045

Next, we use the rand.Int31() function to generate a random Int31 type number and assign it to the randNum variable. Finally, we print the random number using the fmt.Println() function.

Changing the Range of Generated Int31 Type Numbers

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

rand.Int31n(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 Int31 type number between -50 and 50
   randNum := rand.Int31n(50-(-50)+1) + (-50)

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

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

Output

-4

Conclusion

Generating random Int31 type numbers in Golang is easy and straightforward using the rand.Int31() 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: 08-May-2023

125 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements