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
+8.5k Golang : interface - when and where to use examples
+3.9k Golang : A program that contain another program and executes it during run-time
+7.2k Golang : How To Use Panic and Recover
+10.6k Golang : Convert decimal number(integer) to IPv4 address
+72.7k Golang : How to return HTTP status code?
+8.3k Golang : Changing a RGBA image number of channels with OpenCV
+11.8k Golang : Convert IPv4 address to packed 32-bit binary format
+10.6k Golang : Calculations using complex numbers example
+21.6k Golang : Strings to lowercase and uppercase example
+16.1k Golang : delete and modify XML file content
+8.4k Golang : Accessing content anonymously with Tor
+7.6k Golang : Convert(cast) []byte to io.Reader type