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
+8.4k Golang : Check if integer is power of four example
+5.4k Unix/Linux/MacOSx : How to remove an environment variable ?
+15.5k Golang : Find location by IP address and display with Google Map
+10.3k Golang : How to profile or log time spend on execution?
+17.4k Golang : How to tell if a file is compressed either gzip or zip ?
+51.5k Golang : Check if item is in slice/array
+11.6k SSL : The certificate is not trusted because no issuer chain was provided
+25.2k Golang : Create PDF file from HTML file
+7.3k Golang : Use modern ciphers only in secure connection
+12.6k Golang : HTTP response JSON encoded data
+5.7k Javascript : How to refresh page with JQuery ?
+12.4k Golang : Validate email address