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
+9.7k Golang : Create Temporary File
+13.5k Golang : Save(pipe) HTTP response into a file
+18.3k Golang : Close channel after ticker stopped example
+20.4k Golang : Convert seconds to minutes and remainder seconds
+4.4k Python : Delay with time.sleep() function example
+10.6k Golang : Handle API query by curl with Gorilla Queries example
+7.2k Golang : Routes multiplexer routing example with regular expression control
+18.3k Golang : How to Set or Add Header http.ResponseWriter?
+12.5k Golang : Compress and decompress file with compress/flate example
+11.6k Golang : Transform comma separated string to slice example
+17.9k Golang : Get current URL example
+15.3k Golang : Get the IPv4 and IPv6 addresses for a specific network interface