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.5k Golang : Handle sub domain with Gin
+2.3k JQuery : Calling a function inside Jquery(document) block
+2.2k Get website traffic ranking with Similar Web or Alexa
+19k Golang : Validate IP address
+10k Golang : Send email with attachment
+2.8k Golang : Function as an argument type example
+23.5k Golang : Upload and download file to/from AWS S3
+5.2k Golang : Pass database connection to function called from another package and HTTP Handler
+5k Golang : Decompress zlib file example
+8.7k Golang : Check if a string contains multiple sub-strings in []string?
+4.2k Golang : Display a text file line by line with line number example
+4.1k Golang : Clone with pointer and modify value