Software Engineering Wiki

RandIntInRange

Returns a random integer in the specified range.

  • Use rand.Intn() to generate a random number between 0 and the distance between min and max and add min to it.
import "math/rand"

func RandIntInRange(min, max int) int {
	return rand.Intn(max-min) + min
}
RandIntInRange(0, 5) // 2

Last updated 15 September 2026 · Edit this page