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.8k Golang : Set or add headers for many or different handlers
+4.5k Golang : Valued expressions and functions example
+19.3k Mac OSX : Homebrew and Golang
+5.9k Unix/Linux : How to test user agents blocked successfully ?
+38.6k Golang : Converting a negative number to positive number
+19.5k Golang : Get RGBA values of each image pixel
+16.3k Golang : How to check if input from os.Args is integer?
+16.9k Golang : Gzip file example
+10.5k Golang : Detect number of faces or vehicles in a photo
+5.4k Python : Convert(cast) string to bytes example
+11.4k Golang : Fix - does not implement sort.Interface (missing Len method)