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
+3.8k Nginx and PageSpeed build from source CentOS example
+12.8k How to automatically restart your crashed Golang server
+15.5k Golang : Gzip file example
+8.2k Golang : Extract or copy items from map based on value
+5k Golang : How to write backslash in string?
+10.7k Golang : Split strings into command line arguments
+6.1k Web : How to see your website from different countries?
+10.4k Golang : Surveillance with web camera and OpenCV
+29.6k error: trying to remove "yum", which is protected
+12k Golang : Count number of runes in string
+11.3k Golang : Covert map/slice/array to JSON or XML format
+72.7k Golang : How to return HTTP status code?