Logo

Programming-Idioms

History of Idiom 70 > diff from v21 to v22

Edit summary for version 22 by :
[Go] Conciser imports

Version 21

2016-02-22, 15:28:53

Version 22

2016-02-22, 15:29:11

Idiom #70 Use clock as random generator seed

Get the current date and provide it as a seed to a random generator. The generator sequence will be different at each run.

Idiom #70 Use clock as random generator seed

Get the current date and provide it as a seed to a random generator. The generator sequence will be different at each run.

Imports
import (
	"math/rand"
	"time"
)
Imports
import "math/rand"
import "time"
Code
r := rand.New(rand.NewSource(time.Now().UnixNano()))
Code
r := rand.New(rand.NewSource(time.Now().UnixNano()))
Comments bubble
r is of type *rand.Rand.
Comments bubble
r is of type *rand.Rand.
Doc URL
https://golang.org/pkg/math/rand/#NewSource
Doc URL
https://golang.org/pkg/math/rand/#NewSource
Demo URL
http://play.golang.org/p/o1XU6brwr7
Demo URL
http://play.golang.org/p/o1XU6brwr7