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
+25.4k Golang : Convert file content into array of bytes
+5.1k CodeIgniter/PHP : Remove empty lines above RSS or ATOM xml tag
+20.8k Golang : Use TLS version 1.2 and enforce server security configuration over client
+3.8k Javascript : Access JSON data example
+12.1k Golang : Convert IPv4 address to packed 32-bit binary format
+20.1k Golang : Get password from console input without echo or masked
+18.3k Golang : Fix cannot download, $GOPATH not set error
+9.9k Golang : Create Temporary File
+10.1k Golang : Web routing/multiplex example
+14.4k Golang : How to add color to string?
+40.5k Golang : Convert string to array/slice