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
+12.6k Golang : Gin framework accept query string by post request example
+8.4k Golang : ffmpeg with os/exec.Command() returns non-zero status
+6.2k Golang : Example of custom handler for Gorilla's Path usage.
+16.1k Golang : delete and modify XML file content
+81.4k Golang : How to convert character to ASCII and back
+26.8k Golang : Read, Write(Create) and Delete Cookie example
+21k Golang : Repeat a character by multiple of x factor
+7.3k Swift : Convert (cast) Character to Integer?
+6.5k SSL : How to check if current certificate is sha1 or sha2 from command line
+10.6k Golang : Handle API query by curl with Gorilla Queries example
+40.8k Golang : Get hardware information such as disk, memory and CPU usage
+20.8k Golang : Securing password with salt