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
+9.4k Golang : Create unique title slugs example
+5.3k JavaScript/JQuery : Redirect page examples
+18.6k Golang : Set, Get and List environment variables
+12.1k Golang : Find and draw contours with OpenCV example
+8.1k Golang : Sort words with first uppercase letter
+32.5k Golang : Math pow(the power of x^y) example
+6.6k Elasticsearch : Shutdown a local node
+18.9k Golang : How to make function callback or pass value from function as parameter?
+13.6k Golang : reCAPTCHA example
+31.5k Golang : bufio.NewReader.ReadLine to read file line by line
+26.7k Golang : How to check if a connection to database is still alive ?
+8.1k Golang : Variadic function arguments sanity check example