Skip to main content

Command Palette

Search for a command to run...

Generate Random number with math/crypto/rand in Go

Updated
1 min readView as Markdown
Generate Random number with math/crypto/rand in Go
S
DevRel at StackGen | Formerly at Deepfence ,Tenable , Accurics | AWS Community Builder also Docker Community Award Winner at Dockercon2020 | CyberSecurity Innovator of Year 2023 award by Bsides Bangalore | Docker/HashiCorp Meetup Organiser Bangalore & Co-Author of Learn Lightweight Kubernetes with k3s (2019) , Packt Publication & also run Non Profit CloudNativeFolks / CloudSecCorner Community To Empower Free Education reach out me twitterhttps://twitter.com/sangamtwts or just follow on GitHub -> https://github.com/sangam14 for Valuable Resources

Generate Random number with math/crypto/rand in Go - Programming in GO

Random number is useful in many application. One such example is salting password to make in more secure. In this tutorial, we will learn how to generate random number in Go with math/rand library.

package main

 import (
      "fmt"
      "math/rand"
      "time"
 )


 func main() {
    rand.Seed(time.Now().UnixNano())
    fmt.Println(rand.Intn(100))
 }

Run :

 go run math-rand.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.

 package main

 import "encoding/binary"
 import "crypto/rand"

 func main() {
    var n int32
    binary.Read(rand.Reader, binary.LittleEndian, &n)
    println(n)
 }

Run :

 go run crytpo-rand.go
14 views

GopherLabs

Part 1 of 50

Ultimate Series of Blogs for All Golang Developer

More from this blog

C

CloudNativeFolks Community

140 posts

CloudNativeFolks is non profit community that empower and educate about cloud native technology !