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
+24.2k Golang : Upload to S3 with official aws-sdk-go package
+11k Golang : How to transmit update file to client by HTTP request example
+9.8k Golang : Populate slice with sequential integers example
+7.4k Golang : How to iterate a slice without using for loop?
+27.6k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+18.9k Golang : Implement getters and setters
+17k Golang : read gzipped http response
+10.7k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+9.6k Golang : Get all countries currencies code in JSON format
+19k Golang : How to make function callback or pass value from function as parameter?
+5.9k Unix/Linux : How to test user agents blocked successfully ?