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
+23.7k Golang : Read a file into an array or slice example
+14.1k Golang: Pad right or print ending(suffix) zero or spaces in fmt.Printf example
+11.1k Golang : Create S3 bucket with official aws-sdk-go package
+36.2k Golang : Integer is between a range
+7k Golang : Normalize email to prevent multiple signups example
+5.1k Google : Block or disable caching of your website content
+9.1k Golang : What is the default port number for connecting to MySQL/MariaDB database ?
+14.1k Golang : Human readable time elapsed format such as 5 days ago
+15.7k Golang : Validate hostname
+5.9k Unix/Linux : How to test user agents blocked successfully ?
+11.3k Google Maps URL parameters configuration
+9.8k PHP : Get coordinates latitude/longitude from string