Generating Random Numbers in Go

Depending on your use case, another option is the math/rand package. Don’t do this if you’re generating numbers that need to be completely unpredictable. It can be helpful if you need to get results that are reproducible, though — just pass in the same seed you passed in the first time.

Here’s the classic “seed the generator with the current time and generate a number” program:

package main

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

func main() {
    rand.Seed(time.Now().Unix())
    fmt.Println(rand.Int())
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)