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
+11.1k Use systeminfo to find out installed Windows Hotfix(s) or updates
+5.1k Javascript : Shuffle or randomize array example
+10.3k Android Studio : Simple input textbox and intercept key example
+5.9k Golang : Get missing location after unmarshal binary and gob decode time.
+11.1k Golang : How to flush a channel before the end of program?
+27.6k Golang : Decode/unmarshal unknown JSON data type with map[string]interface
+14.1k Android Studio : Use image as AlertDialog title with custom layout example
+23k Golang : Read a file into an array or slice example
+8.6k Golang : Gorilla web tool kit schema example
+6.8k Golang : How to call function inside template with template.FuncMap
+6.3k PHP : Shuffle to display different content or advertisement
+10.8k Golang : Simple image viewer with Go-GTK