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.5k Nginx : Password protect a directory/folder
+18.2k Golang : Measure http.Get() execution time
+13.4k Golang : Submit web forms without browser by http.PostForm example
+6k Golang : Squaring elements in array
+5.7k Get Facebook friends working in same company
+11k Golang : 2 dimensional array example
+6k Golang : Mapping Iban to Dunging alphabets
+9.1k Golang : Text file editor (accept input from screen and save to file)
+11.5k Golang : Get terminal width and height example
+6.6k Setting $GOPATH environment variable for Unix/Linux and Windows
+8.1k Golang : Create and shuffle deck of cards example
+7.8k Golang : Write multiple lines or divide string into multiple lines