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
+33.7k Golang : Proper way to set function argument default value
+7.7k Golang : Gomobile init produce "iphoneos" cannot be located error
+13.3k Golang : Strings comparison
+6.1k CodeIgniter : form input set_value cause " to become & quot
+6.9k Golang : Gorrila mux.Vars() function example
+6.1k Golang : Extract sub-strings
+20.9k Golang : Convert(cast) string to rune and back to string example
+7.1k Golang : Accessing dataframe-go element by row, column and name example
+26.4k Golang : How to check if a connection to database is still alive ?
+9.3k Golang : Read file with ioutil
+19.3k Golang : Example for DSA(Digital Signature Algorithm) package functions
+9.2k Golang : Qt Yes No and Quit message box example