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 : Check to see if *File is a file or directory
+21.9k Golang : Securing password with salt
+12.3k Golang : Exit, terminating or aborting a program
+6.8k Golang : Calculate BMI and risk category
+17.4k Golang : Parse date string and convert to dd-mm-yyyy format
+10.2k Golang : Meaning of omitempty in struct's field tag
+16.3k Golang : Merge video(OpenCV) and audio(PortAudio) into a mp4 file
+13.6k Golang : Convert spaces to tabs and back to spaces example
+25.2k Golang : Daemonizing a simple web server process example
+20.9k Golang : How to force compile or remove object files first before rebuild?
+8.8k Golang : io.Reader causing panic: runtime error: invalid memory address or nil pointer dereference
+5.1k Golang : Generate Interleaved 2 inch by 5 inch barcode