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
+11.1k Golang : Byte format example
+7.1k Golang : Process json data with Jason package
+6k Apt-get to install and uninstall Golang
+13.5k Golang : Get dimension(width and height) of image file
+5.8k Golang : Missing Subversion command
+11.6k Golang : Find and draw contours with OpenCV example
+7.3k Android Studio : AlertDialog to get user attention example
+7.5k Golang : Example of how to detect which type of script a word belongs to
+19.5k Golang : Convert(cast) bytes.Buffer or bytes.NewBuffer type to io.Reader
+35.8k Golang : Converting a negative number to positive number
+7k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+11.3k Golang : Change date format to yyyy-mm-dd