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
+7.1k Golang : Find the shortest line of text example
+27.4k Golang : Find files by name - cross platform example
+7.5k Golang : File system scanning
+5.6k Golang : PGX CopyFrom to insert rows into Postgres database
+21.9k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+11.4k CodeIgniter : How to check if a session exist in PHP?
+19.3k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+29.6k Golang : JQuery AJAX post data to server and send data back to client example
+8.3k Golang : Reverse text lines or flip line order example
+4.8k Linux : sudo yum updates not working
+13.6k Facebook PHP getUser() returns 0
+5.7k PHP : Convert CSV to JSON with YQL example