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
+13.6k Golang : Search folders for file recursively with wildcard support
+6.5k Golang : Detect sample rate, channels or latency with PortAudio
+23.7k Golang : Get current file path of a file or executable
+14.4k Chrome : ERR_INSECURE_RESPONSE and allow Chrome browser to load insecure content
+16.3k Google Chrome : Your connection to website is encrypted with obsolete cryptography
+7k Findstr command the Grep equivalent for Windows
+4.8k Cash Flow : 50 days to pay your credit card debt
+4.6k PHP : Convert CSV to JSON with YQL example
+13.1k Golang : How to get URL port?
+3.1k Golang : PGX CopyFrom to insert rows into Postgres database