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
+32.8k Golang : How to check if a date is within certain range?
+17.3k Golang : Multi threading or run two processes or more example
+10.5k Golang : How to delete element(data) from map ?
+10.1k Golang : How to check if a website is served via HTTPS
+10.4k Generate Random number with math/rand in Go
+35.1k Golang : Strip slashes from string example
+9k Golang : Gonum standard normal random numbers example
+25.1k Golang : Convert uint value to string type
+6.4k Grep : How to grep for strings inside binary data
+18.1k Golang : Get command line arguments
+9.1k Golang : Write multiple lines or divide string into multiple lines