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
+7.8k Golang : Configure Apache and NGINX to access your Go service example
+12.2k Golang : Sort and reverse sort a slice of bytes
+8k Golang: Prevent over writing file with md5 hash
+10.6k Golang : Simple image viewer with Go-GTK
+3.8k Javascript : Empty an array example
+14k Golang : How to determine if user agent is a mobile device example
+9.3k Golang : Populate slice with sequential integers example
+12.2k Android Studio : Highlight ImageButton when pressed on example
+11.7k Golang : Perform sanity checks on filename example
+12.3k Golang : zlib compress file example
+37.5k Golang : Read a text file and replace certain words
+16.2k Golang : File path independent of Operating System