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
+24k Golang : How to validate URL the right way
+23.1k Golang : Read a file into an array or slice example
+7.2k Golang : How to convert strange string to JSON with json.MarshalIndent
+6k Golang : Calculate US Dollar Index (DXY)
+6.6k Swift : substringWithRange() function example
+6.2k CodeIgniter : form input set_value cause " to become & quot
+13.6k Golang : Get dimension(width and height) of image file
+6.8k Golang : Gargish-English language translator
+15.1k Golang : Find location by IP address and display with Google Map
+13.5k Golang : Convert spaces to tabs and back to spaces example
+17.8k Golang : Convert IPv4 address to decimal number(base 10) or integer