Today I Learned

hashrocket A Hashrocket project

Seeding Golang's rand

Random numbers in Go don't seem random? Surprise! The rand package defaults to a seed of 1.

That's great if you need a bunch of random numbers at the start of your program. Not great if you expect a different outcome each time you run the program.

A solution is to seed rand with Unix time. Try it in the init() function:

package main

import (
    "math/rand"
    "time"
)

func init() {
    rand.Seed(time.Now().UTC().UnixNano())
}
See More #go TILs