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
+19k Golang : Get RGBA values of each image pixel
+7.7k Golang : Handle Palindrome string with case sensitivity and unicode
+11.8k Golang : Save webcamera frames to video file
+9.9k Golang : Random Rune generator
+19.1k Golang : Get current URL example
+40.7k Golang : How to check if a string contains another sub-string?
+7.1k Golang : Scanf function weird error in Windows
+5.5k Golang : Error handling methods
+27k Golang : Convert CSV data to JSON format and save to file
+4.8k Golang : Display packages names during compilation
+9k Golang : How to get username from email address
+4.6k Python : Find out the variable type and determine the type with simple test