Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Random with seed

  • Seed

  • time.Now().Unix

  • In order to generate different random numbers one needs to set the starting point by calling Seed.

  • A common way to do that is to provide the number of seconds since the epoch.

package main

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

func main() {
	fmt.Println(time.Now().UnixNano())

	rand.Seed(time.Now().UnixNano())

	a := rand.Float64()

	fmt.Println(a)
}
1586851169266458373
0.2296012220177759