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
+39.2k Golang : How to iterate over a []string(array)
+23.1k Golang : Calculate time different
+22.3k Golang : Repeat a character by multiple of x factor
+15.6k Golang : Get all local users and print out their home directory, description and group id
+16.5k Golang :Trim white spaces from a string
+88.6k Golang : How to convert character to ASCII and back
+13.6k Facebook PHP getUser() returns 0
+17.6k Golang : Linked list example
+39.8k Golang : Remove dashes(or any character) from string
+39.3k Golang : How to read CSV file
+32.7k Golang : Copy directory - including sub-directories and files