Generating a random number between given numbers in GoLang


GoLang has math/rand pakage using which we can generate random numbers.

Following code shows how to write a function in GoLang which accepts min & max values and produce a random number between those two numbers.

import (
  "math/rand"
  "time"
)

func init() {
  rand.Seed(time.Now().UnixNano())
}

func RandomNumber(min int, max int) int {
  return rand.Intn(max-min) + min
}