Generate Random number with math/rand in Go
Random number is useful in many application. One such example is salting password to make in more secure. In this tutorial, we will learn how to generate random number in Go with math/rand library.
File : math-rand.go
package main
import (
"fmt"
"math/rand"
"time"
)
func main() {
rand.Seed(time.Now().UnixNano())
fmt.Println(rand.Intn(100))
}
Run the code:
> go run math-rand.go
and see how it goes. :-)
See also : Random number generation with crypto/rand in Go
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+32.5k Golang : How to check if slice or array is empty?
+37.7k Golang : Convert to io.ReadSeeker type
+6.1k Golang : Get environment variable
+5.1k Golang : Missing Subversion command
+11.3k Golang : Search and extract certain XML data example
+5.1k Golang : Experimenting with the Rejang script
+9.3k Golang : How to profile or log time spend on execution?
+4.2k Swift : Convert (cast) Float to Int or Int32 value
+10.3k Golang : Simple file scaning and remove virus example
+10.9k Golang : Simple client-server HMAC authentication without SSL example
+36.6k Golang : Comparing date or timestamp