Skip to main content

Generate Random number with math/crypto/rand in Go

Updated 1 min readMarkdown
Part of series GopherLabs
Generate Random number with math/crypto/rand in Go

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

9 views

About the author

  • 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

No comments yet. Be the first to comment.

GopherLabs

Part 1 of 50

Ultimate Series of Blogs for All Golang Developer

More from this blog