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
+19.8k Golang : How to read float value from standard input ?
+5.2k Golang : Create new color from command line parameters
+13.5k Golang : Get URI segments by number and assign as variable example
+21.6k Golang : Test file read write permission example
+5.1k Golang : Missing Subversion command
+5.7k Golang : Calculate pivot points for a cross
+9.8k Golang : Sieve of Eratosthenes algorithm
+8.3k Golang : Extract or copy items from map based on value
+14.3k Golang : Delete certain files in a directory
+12.7k Golang : How to check if a file is hidden?
+24.7k Golang : missing Mercurial command
+27.3k Golang : Detect (OS) Operating System