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.1k Golang : GMail API create and send draft with simple upload attachment example
+9.8k Golang : Sort and reverse sort a slice of floats
+14.2k Golang : Compress and decompress file with compress/flate example
+7.6k Golang : Accessing dataframe-go element by row, column and name example
+13.9k Golang : Activate web camera and broadcast out base64 encoded images
+15.1k Golang : How to check for empty array string or string?
+23.1k Golang : Gorilla mux routing example
+10.1k CodeIgniter : Load different view for mobile devices
+9k Golang : Executing and evaluating nested loop in html template
+12.2k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+9.2k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+36.5k Golang : Smarter Error Handling with strings.Contains()