Golang – How to generate a random integer

Small script to generate a random integer, between 1 and 100, in Golang, the Google programming language.

package main

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

func random(min, max int) int {
    rand.Seed(time.Now().UTC().UnixNano())
    return rand.Intn(max-min) + min
}

func main() {

    var myrand int
    myrand = random(1, 101)
    
    fmt.Println(myrand)
    
}

Leave a Comment

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