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.3k Golang : Totalize or add-up an array or slice example
+5.3k Golang : Calculate diameter, circumference, area, sphere surface and volume
+6.5k Golang : Test if an input is an Armstrong number example
+12.2k Javascript : Prompt confirmation before exit
+4.9k Linux/Unix : Commands that you need to be careful about
+12.2k Golang : Tutorial on loading GOB and PEM files
+16.9k Golang : Calculate entire request body length during run time
+28.2k Get client IP Address in Go
+4.3k How to check with curl if my website or the asset is gzipped ?
+28.9k Golang : Download file example
+13.4k Golang : Get digits from integer before and after given position example
+6k Golang : Calculate how many weeks left to go in a given year