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.9k Golang : Channels and buffered channels examples
+13.7k Golang : Check if an integer is negative or positive
+6.5k Golang : How to determine if request or crawl is from Google robots
+26.3k Golang : Convert(cast) string to uint8 type and back to string
+5.8k Golang : List all packages and search for certain package
+10.3k Golang : Convert file unix timestamp to UTC time example
+7k Golang : A simple forex opportunities scanner
+12.1k Golang : Split strings into command line arguments
+7.6k Golang : Command line ticker to show work in progress
+10.2k Golang : Random Rune generator
+11.6k Get form post value in Go
+35k Golang : Upload and download file to/from AWS S3