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
+9.8k Golang : Use regular expression to get all upper case or lower case characters example
+24.9k Golang : Get current file path of a file or executable
+5.9k Golang : Get Hokkien(福建话)/Min-nan(閩南語) Pronounciations
+12.7k Golang : Convert(cast) uintptr to string example
+7.2k Golang : Dealing with struct's private part
+17.5k Golang : Login and logout a user after password verification and redirect example
+30.2k Golang : Remove characters from string example
+7.8k Golang : Append and add item in slice
+21k Curl usage examples with Golang
+8.3k Linux/Unix : fatal: the Postfix mail system is already running
+21.5k Golang : How to reverse slice or array elements order
+9.7k CodeIgniter : Load different view for mobile devices