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
+12k Golang : List objects in AWS S3 bucket
+3.8k Linux/MacOSX : How to symlink a file?
+4.5k Clean up Visual Studio For Mac installation failed disk full problem
+15.1k Golang : Check if a string contains multiple sub-strings in []string?
+8.9k Golang : Compare files modify date example
+8.2k Golang : Detect number of active displays and the display's resolution
+18.9k Golang : How to get struct tag and use field name to retrieve data?
+6.3k Golang : File system scanning
+35k Golang : Display float in 2 decimal points and rounding up or down
+15.7k Golang : How to tell if a file is compressed either gzip or zip ?