Logo

Programming-Idioms

History of Idiom 70 > diff from v18 to v19

Edit summary for version 19 by :
New Java implementation by user [lck]

Version 18

2016-01-04, 04:27:22

Version 19

2016-02-18, 16:58:01

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
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
Imports
import (
	"math/rand"
	"time"
)
Imports
import (
	"math/rand"
	"time"
)
Code
rand.Seed(time.Now().UnixNano())
Code
rand.Seed(time.Now().UnixNano())
Comments bubble
This initializes the default Source.
Comments bubble
This initializes the default Source.
Doc URL
https://golang.org/pkg/math/rand/#Seed
Doc URL
https://golang.org/pkg/math/rand/#Seed
Demo URL
http://play.golang.org/p/3sk_hOz0PH
Demo URL
http://play.golang.org/p/3sk_hOz0PH