Golang : Random integer with rand.Seed() within a given range

Tags : golang random number integer rand-seed

Need to generate a random single integer within a given number range. For example, get one random integer from 1 to 10.

The Golang code to do this :

 package main

 import (

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

 func main() {
 num := numRand(1, 10)

 fmt.Println("Number is : ", num)

NOTE : Executing this code in play.golang.org WILL NOT work.

Sample output :

Number is : 9

Number is : 4

Number is : 5

Number is : 1

  See also : Golang : Generate random integer or float number

Tags : golang random number integer rand-seed

By Adam Ng
