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
+6.9k Golang : How To Use Panic and Recover
+42.9k Golang : Read tab delimited file with encoding/csv package
+10.3k Golang : Calculations using complex numbers example
+16.3k Golang : Find IP address from string
+8k Golang : How to check if a website is served via HTTPS
+12.1k Golang : Fix image: unknown format error
+8.3k Golang : Sort and reverse sort a slice of integers
+33k Golang : Validate IP address
+17.4k Golang : convert int to string
+10.1k Get form post value in Go
+4.5k Golang : How to search a list of records or data structures
+4.6k Golang : Denco multiplexer example