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
+27.5k Golang : Saving(serializing) and reading file with GOB
+12.7k Golang : Get current time
+6.7k Golang : Reverse a string with unicode
+12.6k Golang : Gin framework accept query string by post request example
+7.3k Golang : How to check if input string is a word?
+3.8k Nginx and PageSpeed build from source CentOS example
+23.7k Golang : Convert long hexadecimal with strconv.ParseUint example
+12.5k Golang : How to check if a file is hidden?
+16.4k Golang : Read data from config file and assign to variables
+50.5k Golang : How to get time in milliseconds?
+14k Golang : Get digits from integer before and after given position example
+5k Golang : How to verify input is rune?