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
+26.9k Golang : bufio.NewReader.ReadLine to read file line by line
+6.5k Golang : Ackermann function example
+5.5k Golang : Dealing with postal or zip code example
+22.2k Golang : Change file read or write permission example
+6.5k Golang : Routes multiplexer routing example with regular expression control
+6.3k Golang : Load DSA public key from file example
+7k Golang : Simple histogram example
+9.6k Golang : Simple client-server HMAC authentication without SSL example
+4k Golang : If else example and common mistake
+6.6k Golang : HTTP Server Example
+9.9k Golang : Clean formatting/indenting or pretty print JSON result
+7.5k Golang : Format strings to SEO friendly URL example