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
+6.2k Golang : Totalize or add-up an array or slice example
+16k Golang :Trim white spaces from a string
+28k Get file path of temporary file in Go
+14.9k Golang : How to add color to string?
+20k Nginx + FastCGI + Go Setup.
+22.9k Golang : Print out struct values in string format
+9.7k Golang : Translate language with language package example
+6.5k Golang : Muxing with Martini example
+20.4k Golang : Convert PNG transparent background image to JPG or JPEG image
+4.4k Facebook : How to place save to Facebook button on your website
+8.5k Golang : Gorilla web tool kit schema example
+13.1k Golang : Read from buffered reader until specific number of bytes