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
+3k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+12.6k Golang : How to implement two-factor authentication?
+29.4k Golang : Display float in 2 decimal points and rounding up or down
+10.5k Golang : syscall.Socket example
+4k Golang : Detect variable or constant type
+4.9k Golang : Array mapping with Interface
+8.5k Golang : Listen and Serve on sub domain example
+9.9k Golang : HTTP response JSON encoded data
+27k Golang : Convert []string to []byte examples
+20.3k Golang : Converting a negative number to positive number
+4.9k Golang : Gonum standard normal random numbers example
+4.1k Linux : How to fix Brother HL-1110 printing blank page problem