- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Get Uint64 Type Random Number in Golang?
In Golang, generating random numbers is a common task for many developers. While there are various methods for generating random numbers, it is important to use the correct type of number for your particular use case. In this article, we will focus on generating Uint64 type random numbers in Golang.
Step 1: Importing the Math/rand Package
To generate random Uint64 numbers, we need to import the "math/rand" package into our Golang program. This package contains a set of functions for generating pseudo-random numbers.
Step 2: Setting the Seed
The next step is to set the seed for the random number generator. This is done using the "rand.Seed" function. The seed is used to initialize the pseudo-random number generator with a starting value.
It is important to note that if you don't set the seed, the generator will use the same starting value every time you run your program. This means that you will get the same sequence of random numbers each time. To avoid this, you can use a time-based seed value, as shown below −
import ( "math/rand" "time" ) func main() { rand.Seed(time.Now().UnixNano()) }
Step 3: Generating the Random Uint64 Number
Once the seed is set, we can use the "rand.Uint64" function to generate the random Uint64 number. This function takes no parameters and returns a pseudo-random Uint64 value.
Example
Here's an example of generating a random Uint64 number −
package main import ( "fmt" "math/rand" "time" ) func main() { rand.Seed(time.Now().UnixNano()) randomUint64 := rand.Uint64() fmt.Println(randomUint64) }
Output
17611704042530021577
Step 4: Generating a Range of Uint64 Numbers
In some cases, you may want to generate a range of Uint64 numbers. This can be done by using the "rand.Intn" function, which returns a random integer between 0 and n-1.
Example
Here's an example of generating a range of random Uint64 numbers −
package main import ( "fmt" "math/rand" "time" ) func main() { rand.Seed(time.Now().UnixNano()) for i := 0; i < 10; i++ { randomUint64 := rand.Intn(100000000000000000) fmt.Println(randomUint64) } }
In this example, we generate 10 random Uint64 numbers between 0 and 99,999,999,999,999,999.
Output
79048108235541454 86858524305700537 49387909184887176 82420268710562967 39756672912880085 15115054020839883 73878099799920809 63372265888315045 57325136215408552 286418938921796
Conclusion
Generating random Uint64 numbers in Golang is a simple process that involves setting the seed and using the "rand.Uint64" function. By following the steps outlined in this article, you can generate a single Uint64 number or a range of numbers for your particular use case. Remember to always set the seed to ensure that you get different sequences of random numbers each time you run your program.