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
+7.8k Golang : Handle sub domain with Gin
+6.5k Golang : Detect sample rate, channels or latency with PortAudio
+23.9k Golang : Convert uint value to string type
+22.9k Golang : How to print rune, unicode, utf-8 and non-ASCII CJK(Chinese/Japanese/Korean) characters?
+18.3k Golang : Close channel after ticker stopped example
+7.3k Useful methods to access blocked websites
+8.2k Golang : Detect number of active displays and the display's resolution
+3.1k Detect if Google Analytics and Developer Media are loaded properly or not
+6.2k Nginx : How to block user agent ?
+6.5k Golang : Set horizontal, vertical scroll bars policies and disable interaction on Qt image
+5.8k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+5.7k Golang : Warp text string by number of characters or runes example