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.9k Golang : Decode XML data from RSS feed
+8.6k Golang : Turn string or text file into slice example
+12.8k Golang : How to check if your program is running in a terminal
+5k Golang : How to verify input is rune?
+9.2k Golang : cannot assign type int to value (type uint8) in range error
+5k Fix ERROR 2003 (HY000): Can't connect to MySQL server on 'IP address' (111)
+12.8k Golang : Simple word wrap or line breaking example
+5.1k Golang : Missing Subversion command
+25.1k Golang : Encrypt and decrypt data with AES crypto
+20.7k Golang : How to run Golang application such as web server in the background or as daemon?
+17k Golang : Find IP address from string
+12.5k Golang : Human readable time elapsed format such as 5 days ago