Introduction to Golang Random Number
In Go language, the Random number is one way to produce the numbers with a random value, to do so we can use the package math/rand and from this we can use two functions one is int() and another is int31, both of these functions produces random numbers for us, int() function will take one argument as the number which instructs to generate non-negative numbers for us, similarly we can use the function int31(), which will generate a random number of 31 bit, we can also customize these numbers according to our uses by adding some more formats to these numbers.
Syntax
There are basically two ways to generate a random number using two functions of the go language, they are int() and int31(), in the below we have written the syntax for both of them, let us discuss in steps this syntax.
- Before using the random number in go language first we need to import the rand package.
- Next on the rand, we are calling two author methods like rand.int(n) and rand.int31().
- Once we make this call it will generate the random number for us.
rand.Intn(n int) int //example rand.intn(4)
rand.Int31() int31 //example rand.int31()
How do Random Numbers Work in Go language?
Before going to understand the core concept of the working of the random number in go language first we need to understand the uses of the random number. You have seen many applications used to send OTP for registrations and login to that application, so these applications use the logic of random number generation. In case of a very huge number of users, it may be possible that the generated random number can be duplicated with any already send a random number so to avoid such type of situation we are using the go language system defined random number generation logic. Let me explain the working of the generation of the random number in the go language with the help of the below steps.
- Before using random generation in go language we have to import the math/rand package into the application.
- After importing the package we will have two options to generate the random number one is by using the function int() and another is with the help of the function int31().
- Bothe the function used to generate the random number for us, the only difference is that in case of the int() we can pass some number where we can define the specification of the number length and in case of int31() function we can not pass any custom value to it, it will automatically generate the number with a length of 31 bit.
- We can also customize the length of the random number generated by these functions by adding and overriding the logic of generation of the number, we will see this in the example section.
Examples to Implement Golang Random Number
We have given some of the important examples explaining random numbers in go language. If we wanted to run the example then we can create a file with name random.go and copy any example and paste on the file and run the command go run random.go and we can get the output.
Example #1
Below is a simple example using int31() to generate the random number.
Code:
package main
import (
"fmt"
"math/rand"
)
func main() {
random_no1 := rand.Int31()
random_no2 := rand.Int31()
random_no3 := rand.Int31()
random_no4 := rand.Int31()
fmt.Println("The first random number is: ", random_no1)
fmt.Println("The second random number is: ", random_no2)
fmt.Println("The third random number is: ", random_no3)
fmt.Println("The fourth random number is: ", random_no4)
}
Output:
Example #2
In the below example we are generating a random number with the help of the function int31(). Here we have used the customized way to generate the random number by adding some attributes on the random number generated by int31().
Code:
package main
import (
"fmt"
"math/rand"
)
func customised(v1, v2 int32) int32 {
return v1 + v2 + rand.Int31()
}
func main() {
random_no1 := customised(3, 8)
random_no2 := customised(46, 18)
random_no3 := customised(300, 98)
random_no4 := customised(500, 89)
fmt.Println("The first random number is: ", random_no1)
fmt.Println("The second random number is: ", random_no2)
fmt.Println("The third random number is: ", random_no3)
fmt.Println("The fourth random number is: ", random_no4)
}
Output:
Example #3
Below is an example to generate the random number using the function int() and this function will return the random number, we have passed some number to it which defines the random number length.
Code:
package main
import (
"fmt"
"math/rand"
)
func main() {
random_no1 := rand.Intn(6)
random_no2 := rand.Intn(3)
random_no3 := rand.Intn(9)
random_no4 := rand.Intn(11)
fmt.Println("The first random number is: ", random_no1)
fmt.Println("The second random number is: ", random_no2)
fmt.Println("The third random number is: ", random_no3)
fmt.Println("The fourth random number is: ", random_no4)
}
Output:
Example #4
Below is an example where we are generating a random number using the function int(). Here we are trying to customize the random number generated by the function int() by adding some more attributes to it.
Code:
package main
import (
"fmt"
"math/rand"
)
func customised(v1, v2 int) int {
return v1 + v2 + rand.Intn(4)
}
func main() {
random_no1 := customised(4,9)
random_no2 := customised(5,6)
random_no3 := customised(7,11)
random_no4 := customised(8,4)
fmt.Println("The first random number is: ", random_no1)
fmt.Println("The second random number is: ", random_no2)
fmt.Println("The third random number is: ", random_no3)
fmt.Println("The fourth random number is: ", random_no4)
}
Output:
Conclusion
From this tutorial we learned the basic concept of the random number in the go language, we learned the working of the random number in the go language. We focus on the syntax and some of the important examples and uses of the random number in go language.
Recommended Article
This is a guide to Golang Random Number. Here we discuss the introduction, syntax, and working of a random number in golang along with examples and code implementation. You can also go through our other suggested articles to learn more –