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
+10.3k Golang : Get login name from environment and prompt for password
+7.1k Nginx : Password protect a directory/folder
+16.6k Golang : Send email and SMTP configuration example
+6.4k Apt-get to install and uninstall Golang
+8.3k Golang : How To Use Panic and Recover
+7.8k Javascript : Push notifications to browser with Push.js
+13.7k Golang : Query string with space symbol %20 in between
+11.7k Swift : Convert (cast) Float to String
+7.8k Golang : get the current working directory of a running program
+12.8k Golang : Sort and reverse sort a slice of bytes
+34.1k Golang : Call a function after some delay(time.Sleep and Tick)
+12.2k Golang : Save webcamera frames to video file