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.2k Golang : Number guessing game with user input verification example
+6.7k Setting $GOPATH environment variable for Unix/Linux and Windows
+15.4k CodeIgniter/PHP : Create directory if does not exist example
+19.5k Golang : Convert PNG transparent background image to JPG or JPEG image
+11.5k Golang : Exit, terminating or aborting a program
+22.7k Golang : Upload to S3 with official aws-sdk-go package
+4.1k Golang : micron to centimeter example
+7.8k nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
+6.4k Golang : Create zip/ePub file without compression(use Store algorithm)
+6.2k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+4.9k Cash Flow : 50 days to pay your credit card debt
+5k Golang : Shuffle array of list