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
+5.7k PHP : Convert CSV to JSON with YQL example
+19.2k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+13.8k Golang : Qt progress dialog example
+12.2k Golang : convert(cast) string to integer value
+17.6k Golang : Linked list example
+7.4k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+7.6k Golang : How to stop user from directly running an executable file?
+10.1k Golang : Check if user agent is a robot or crawler example
+14k Golang : unknown escape sequence error
+29.9k Golang : Record voice(audio) from microphone to .WAV file
+14.1k Generate salted password with OpenSSL example
+5.7k Swift : Get substring with rangeOfString() function example