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
+5.8k Get website traffic ranking with Similar Web or Alexa
+5.9k Golang : Shuffle array of list
+8.3k Golang : Emulate NumPy way of creating matrix example
+9.8k Golang : Qt get screen resolution and display on center example
+7.9k Setting $GOPATH environment variable for Unix/Linux and Windows
+12.3k Golang : Get month name from date example
+14k Golang : Fix cannot use buffer (type bytes.Buffer) as type io.Writer(Write method has pointer receiver) error
+6.1k PageSpeed : Clear or flush cache on web server
+48.6k Golang : Upload file from web browser to server
+16.3k Golang : Loop each day of the current month example
+20.9k PHP : Convert(cast) int to double/float
+9.6k Golang : How to generate Code 39 barcode?