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
+10.6k Golang : Convert(cast) float to int
+13.1k Golang : Reset buffer example
+7.8k Golang : Get curl -I or head data from URL example
+3.7k Linux : sudo yum updates not working
+3.8k Linux/MacOSX : How to symlink a file?
+7.2k How to show different content from website server when AdBlock is detected?
+7.4k Golang : Count leading or ending zeros(any item of interest) example
+10k Golang : Concatenate (combine) buffer data example
+5.5k Golang : Calculate diameter, circumference, area, sphere surface and volume
+10.8k Golang : Convert a rune to unicode style string \u
+14.9k Golang : Get file permission