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.5k Golang : How to check if your program is running in a terminal
+24k Golang : Find biggest/largest number in array
+16.4k CodeIgniter/PHP : Create directory if does not exist example
+11.7k Golang : Gorilla web tool kit secure cookie example
+30.4k Get client IP Address in Go
+9.9k Golang : Check if user agent is a robot or crawler example
+6.7k Golang : Check if password length meet the requirement
+8.6k Python : Fix SyntaxError: Non-ASCII character in file, but no encoding declared
+5.3k PHP : Hide PHP version information from curl
+5.9k Golang : Detect variable or constant type
+15.7k Golang : Intercept Ctrl-C interrupt or kill signal and determine the signal type
+13.9k Golang : Get dimension(width and height) of image file