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
+4.8k Golang : Error handling methods
+26.6k Golang : Change a file last modified date and time
+14.6k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+11.1k Golang : Display list of countries and ISO codes
+5.2k Golang : Experimental emojis or emoticons icons programming language
+6.4k Golang : Rename part of filename
+12.1k Golang : Convert(cast) int to int64
+9.6k Golang : Bubble sort example
+8.9k Golang : Find and replace data in all files recursively
+19.2k Golang : Pipe output from one os.Exec(shell command) to another command
+3.9k PHP : Extract part of a string starting from the middle
+6.4k Golang : Create zip/ePub file without compression(use Store algorithm)