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
+14.3k Golang : Convert(cast) int to float example
+7.3k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+4.7k Golang : PGX CopyFrom to insert rows into Postgres database
+11.9k Golang : md5 hash of a string
+27.1k Golang : dial tcp: too many colons in address
+9.7k Golang : Ordinal and Ordinalize a given number to the English ordinal numeral
+11.1k Use systeminfo to find out installed Windows Hotfix(s) or updates
+11.3k Golang : Change date format to yyyy-mm-dd
+8.9k Golang : Gonum standard normal random numbers example
+6.6k Get Facebook friends working in same company
+9.5k PHP : Get coordinates latitude/longitude from string
+15.3k Golang : How to convert(cast) IP address to string?