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
+12.9k Golang : Convert(cast) int to float example
+8.9k Golang : Convert file unix timestamp to UTC time example
+7.3k Golang : Convert(cast) []byte to io.Reader type
+28.2k Get client IP Address in Go
+11.7k Golang : error parsing regexp: invalid or unsupported Perl syntax
+24.7k Golang : Encrypt and decrypt data with AES crypto
+10.8k Golang : Remove or trim extra comma from CSV
+3.7k MariaDB/MySQL : Form select statement or search query with Chinese characters
+15.1k CodeIgniter/PHP : Create directory if does not exist example
+30.1k Golang : Validate email address with regular expression
+44.6k Golang : Convert int to byte array([]byte)
+9.8k Golang : Fuzzy string search or approximate string matching example