Skip to content

Latest commit

 

History

History
21 lines (15 loc) · 518 Bytes

seeding-golangs-rand.md

File metadata and controls

21 lines (15 loc) · 518 Bytes

Seeding Golang's Rand

'Random' numbers in Go don't always seem random. This is because 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())
}
...