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.4k Golang : Totalize or add-up an array or slice example
+22.3k Golang : How to read JPG(JPEG), GIF and PNG files ?
+12.4k Golang : Arithmetic operation with numerical slices or arrays example
+4.8k Unix/Linux : secure copying between servers with SCP command examples
+7.4k Golang : How to handle file size larger than available memory panic issue
+33.5k Golang : All update packages with go get command
+11k CodeIgniter : How to check if a session exist in PHP?
+5.2k Golang : Get FX sentiment from website example
+6.4k Golang : Spell checking with ispell example
+14.6k Golang : Normalize unicode strings for comparison purpose
+6.3k CodeIgniter : form input set_value cause " to become & quot
+5.5k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)