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
+41.1k Golang : Unmarshal JSON from http response
+2.7k Python : Find out the variable type and determine the type with simple test
+3.3k Linux/Unix/PHP : Restart PHP-FPM
+31.8k Golang : Remove dashes(or any character) from string
+5.1k Golang : Regular Expression find string example
+4.4k Fix sudo yum hang problem with no output or error messages
+48.4k Golang : Check if item is in slice/array
+2.9k Javascript : Change page title to get viewer attention
+5.9k Golang : Write multiple lines or divide string into multiple lines
+5k Golang : How to check if input string is a word?
+5.9k Golang : How to use Gorilla webtoolkit context package properly