Generate Random number with math/rand in Go
Tags : random-number
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
Tags : random-number
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
+8.3k Golang : Get file permission
+3k Golang : Error reading timestamp with GORM or SQL driver
+6.9k Golang : Use TLS version 1.2 and enforce server security configuration over client
+3.3k Golang : How to pipe input data to executing child process?
+1.7k Golang : Scramble and unscramble text message by randomly replacing words
+6.9k Golang : Drop cookie to visitor's browser and http.SetCookie() example
66 Golang : Executing and evaluating nested loop in html template
+26.1k Golang : convert(cast) bytes to string
+8.3k Golang : Simple client server example
+1.6k Fix yum-complete-transaction error
+2.4k Golang : Get expvar(export variables) to work with multiplexer
+3.6k Golang : rune literal not terminated error