# Random timestamp within last 3 relative years

I'm trying to generate a random time between now and 3 years ago. To my knowledge, Go doesn't have a random(min,max) feature in either math/rand or crypto/rand where min > 0. To naively combat that, I generate a slice containing every time stamp within the relative now and 3 years ago, and then select the index at random with rand.Intn(len(timeRange)), because I am guaranteed to get a random index between 0 and len(timeRange). It works just fine, and I get a Unix time stamp sometime between now and 3 years ago. However, I want to generate just one timestamp, not ~94m of them and then randomly picking one...it's egregiously slow.

Here is the code:

package main

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

func randomTimestamp() time.Time {
now := time.Now().Unix()
threeYearsAgo := int64(now - 94608000)
timeRange := make([]int64,1)
for i := threeYearsAgo; i < now; i++ {
timeRange = append(timeRange, i)
}
randomTime := rand.Intn(len(timeRange))

randomNow := time.Unix(timeRange[randomTime], 0)

return randomNow
}

func main() {
for i := 0; i < 25; i++ {
fmt.Println(randomTimestamp().Format(time.RFC3339))
}
}

How can I optimise randomTimestamp() so it doesn't have to calculate every second over the last 3 years, but it randomly picks one between now and 3 years ago?

• I don't know the in's and out's of your language, but from an abstract logic point of view, surely you can just select a random integer between 0 and 94608000, then subtract your integer from now Commented Dec 3, 2016 at 11:16

As Darren H has said in the comments, just generate a random number between 0 and nanoseconds in three years, and subtract from time.Now():

const threeYears = 3 * 365 * 24 * time.Hour

func main() {
rand.Seed(time.Now().UnixNano())
d := time.Duration(rand.Int63n(int64(threeYears)))