Logo

Programming-Idioms

History of Idiom 70 > diff from v16 to v17

Edit summary for version 17 by :
[Go] +DocURL to rand.NewSource

Version 16

2015-12-30, 17:04:22

Version 17

2015-12-30, 17:06:41

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"
	"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
Demo URL
http://play.golang.org/p/o1XU6brwr7
Demo URL
http://play.golang.org/p/o1XU6brwr7