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
+15.7k Golang : Get checkbox or extract multipart form data value example
+19.3k Golang : Get RGBA values of each image pixel
+8.3k Golang : Auto-generate reply email with text/template package
+6.5k Grep : How to grep for strings inside binary data
+5.6k PHP : Convert CSV to JSON with YQL example
+7.4k Golang : Accessing dataframe-go element by row, column and name example
+13.4k Golang : Get constant name from value
+27.2k Golang : Find files by name - cross platform example
+7.7k Golang : Mapping Iban to Dunging alphabets
+19.9k Golang : How to get time from unix nano example
+6.6k Golang : Warp text string by number of characters or runes example