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
+13.7k Golang : Compress and decompress file with compress/flate example
+30.5k Golang : Download file example
+24.2k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+5.1k Golang : Pad file extension automagically
+15.3k Golang : Validate hostname
+7.7k Golang : Gomobile init produce "iphoneos" cannot be located error
+21.7k Golang : Join arrays or slices example
+21.5k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+19.9k Golang : Check if os.Stdin input data is piped or from terminal
+16.1k Golang : Find out mime type from bytes in buffer
+15.3k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+6.7k Golang : Normalize email to prevent multiple signups example