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.9k Golang : Apply Histogram Equalization to color images
+9.2k Golang : How to profile or log time spend on execution?
+25k Golang : Convert file content into array of bytes
+14.7k Golang : Read a file line by line
+10.7k Golang : Clean formatting/indenting or pretty print JSON result
+16.2k Golang : How to remove certain lines from a file
+18.8k Golang : Read directory content with os.Open
+8k Golang : Generate random Chinese, Japanese, Korean and other runes
+16.5k Golang : Read data from config file and assign to variables
+7.9k Golang : Find the length of big.Int variable example
+16.1k Golang : How to make a file read only and set it to writable again?
+11.4k Golang : "https://" not allowed in import path