Random number generation with crypto/rand in Go
Random number is useful in many applications. From salting password to enabling secure transactions.
In this tutorial, we will learn how to generate random number in Go with crypto/rand library.
File : crypto-rand.go
package main
import "encoding/binary"
import "crypto/rand"
func main() {
var n int32
binary.Read(rand.Reader, binary.LittleEndian, &n)
println(n)
}
Run the code: > go run crytpo-rand.go
and see how it goes. :-)
Reference:
See also : Generate Random number with math/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
+7k Nginx : Password protect a directory/folder
+5.5k Golang : If else example and common mistake
+7.4k Golang : Individual and total number of words counter example
+15.5k Golang : invalid character ',' looking for beginning of value
+8.1k Golang : What fmt.Println() can do and println() cannot do
+4.5k Java : Generate multiplication table example
+31.7k Golang : Example for ECDSA(Elliptic Curve Digital Signature Algorithm) package functions
+12.2k Golang : Sort and reverse sort a slice of runes
+8.4k Golang : Configure Apache and NGINX to access your Go service example