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
+3.7k Golang : Dealing with backquote
+5.9k Golang : Simple histogram example
+10k Golang : convert(cast) string to integer value
+7.2k JavaScript/JQuery : Detect or intercept enter key pressed example
+6.7k Golang : Timeout example
+5.2k Golang : Individual and total number of words counter example
+13.2k Golang : Get file permission
+8k Golang : Detect user location with HTML5 geo-location
+35.3k Golang : Convert string to array/slice
+13.9k Golang : Read a file into an array or slice example
+14.5k Golang : Clean up null characters from input data
+20k Golang : Get current file path of a file or executable