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
+6.3k Golang : Spell checking with ispell example
+9.6k Golang : Get current, epoch time and display by year, month and day
+42.7k Golang : Convert []byte to image
+19.6k Golang : Accept input from user with fmt.Scanf skipped white spaces and how to fix it
+8.2k PHP : How to parse ElasticSearch JSON ?
+30.5k Golang : Interpolating or substituting variables in string examples
+13.4k Golang : Activate web camera and broadcast out base64 encoded images
+8.5k Golang : Gorilla web tool kit schema example
+12.3k Golang : Send data to /dev/null a.k.a blackhole with ioutil.Discard
+30.7k Golang : Calculate percentage change of two values
+10.3k Golang : How to unmarshal JSON inner/nested value and assign to specific struct?
+10.1k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)