Skip to content

How to Generate a Random Int Over a Range (func)

In Golang, there is no function to generate random numbers over a range! Writing a random generator function is easy and I wrote one for you.

Enjoy this simple example:

package main

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

func main() {
	min := 10
	max := 100

	fmt.Println(randInt(min, max))
}

func randInt(min, max int) int {
	rand.Seed(time.Now().UnixNano())

	return rand.Intn(max-min+1) + min
}

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *