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.3k Golang : Pagination with go-paginator configuration example
+11.3k Golang : Simple image viewer with Go-GTK
+8.4k Swift : Convert (cast) Character to Integer?
+12.5k Golang : How to display image file or expose CSS, JS files from localhost?
+18.1k Golang : Simple client server example
+4.8k Javascript : Access JSON data example
+10.5k Golang : Generate random integer or float number
+16.1k Golang : Update database with GORM example
+11k Golang : Get UDP client IP address and differentiate clients by port number
+5.4k JavaScript/JQuery : Redirect page examples
+19.7k Golang : Example for DSA(Digital Signature Algorithm) package functions
+10.7k Golang : Generate 403 Forbidden to protect a page or prevent indexing by search engine