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
+6.2k Golang : Of hash table and hash map
+9.6k Golang : Get UDP client IP address and differentiate clients by port number
+6.8k Golang : Reverse a string with unicode
+8.4k Golang : ffmpeg with os/exec.Command() returns non-zero status
+20.2k SSL : How to check if current certificate is sha1 or sha2
+16.2k Golang : How to remove certain lines from a file
+4.6k Golang : ROT32768 (rotate by 0x80) UTF-8 strings example
+5.6k Golang : When to use make or new?
+25.2k Golang : Find files by extension
+21.2k Generate checksum for a file in Go
+6.7k Gogland : Where to put source code files in package directory for rookie
+4.8k Fix fatal error: evacuation not done in time problem