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
+17.3k Golang : Parse date string and convert to dd-mm-yyyy format
+12k Golang : calculate elapsed run time
+10.2k Golang : Create matrix with Gonum Matrix package example
+11.6k Golang : Convert decimal number(integer) to IPv4 address
+6.9k Golang : Check if one string(rune) is permutation of another string(rune)
+15.3k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+16.5k Golang : Get own process identifier
+5.2k Golang : Display advertisement images or strings on random order
+12.6k Golang : Get terminal width and height example
+4.7k Google : Block or disable caching of your website content
+5.6k Golang : Markov chains to predict probability of next state example