Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I Seed random number generator on official Tour of Go?

The official tour of Go gives the following code in the sandbox:

package main

import (
    "fmt"
    "math/rand"
)

func main() {
    fmt.Println("My favorite number is", rand.Intn(10))
}

And this instruction:

Note: the environment in which these programs are executed is deterministic, so each time you run the example program rand.Intn will return the same number. (To see a different number, seed the number generator; see rand.Seed.)

After reading the entry under the official documentation for rand.Seed and reading this answer, I still can't correctly seed the random number generator.

Can someone please demonstrate how the rand.Seed function should be used to seed the random number generator?

Many thanks, Jon

like image 446
jonsanders101 Avatar asked Jul 23 '17 19:07

jonsanders101


People also ask

What is seed in random number generator Golang?

In Golang, the rand. Seed() function is used to set a seed value to generate pseudo-random numbers. If the same seed value is used in every execution, then the same set of pseudo-random numbers is generated. In order to get a different set of pseudo-random numbers, we need to update the seed value.

How do you generate a random number in Golang?

Golang has built-in support for random number generation in the standard library. Specifically there is the math/rand package which implements pseudo-random number generators. Intn returns, as an int, a non-negative pseudo-random number in [0,n) from the default Source. It panics if n <= 0.

What is set seed in random?

The seed() method is used to initialize the random number generator. The random number generator needs a number to start with (a seed value), to be able to generate a random number. By default the random number generator uses the current system time.

What code is needed to seed the random number generator using the computer's time?

Thus using the two functions, rand () and srand () we can generate random numbers in C++. The function srand () is used to provide seed for generating random numbers while rand () function generates the next random number in the sequence.


2 Answers

By default rand.Intn uses the globalRand.Intn. Its created internally, refer here. So when you set via rand.Seed

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

Then globalRand uses the new seed value.

When needed you can create your own rand generator with seed value. Refer to godoc example.


Play Link (without seed): https://play.golang.org/p/2yg7xjvHoJ

Output:

My favorite number is 1
My favorite number is 7
My favorite number is 7
My favorite number is 9
My favorite number is 1
My favorite number is 8
My favorite number is 5
My favorite number is 0
My favorite number is 6

Play Link (with seed): https://play.golang.org/p/EpW6R5rvM4

Output:

My favorite number is 0
My favorite number is 8
My favorite number is 7
My favorite number is 2
My favorite number is 3
My favorite number is 9
My favorite number is 4
My favorite number is 7
My favorite number is 8

EDIT:

As @AlexanderTrakhimenok mentioned, in playground program execution is deterministic. However, the playground doesn't stop you from supplying rand.Seed value.

Remember Seed value is int64.

When you rand.Intn, it uses default seed value 1 for globalRand.

var globalRand = New(&lockedSource{src: NewSource(1).(Source64)})

And in playground time.Now().UTC().UnixNano() gives you same value 1257894000000000000 since the start time is locked to a constant. But it is different from default seed value, that's why second playground link produces the different result.

So above two would produce the same result always.

How should we change the result in playground?

Yes, we can. Let's supply UnixNano() value 1500909006430687579 to rand.Seed, which is generated from my machine.

Play Link: https://play.golang.org/p/-nTydej8YF

Output:

My favorite number is 3
My favorite number is 5
My favorite number is 3
My favorite number is 8
My favorite number is 0
My favorite number is 5
My favorite number is 4
My favorite number is 7
My favorite number is 1
like image 98
jeevatkm Avatar answered Oct 19 '22 19:10

jeevatkm


As you quoted yourself:

the environment in which these programs are executed is deterministic.

So the Go Playground by design does not allow to create truly pseudo-random outputs.

This is done intentionally for purpose of caching results to minimize CPU/memory usage for consequent runs. So the engine can evaluate your program just once and serve the same cached output every time when you or anyone else run it again.

For the same purpose the start time is locked to a constant.

You may want to read a blog post on how and why it's implemented this way: https://blog.golang.org/playground

like image 37
Alexander Trakhimenok Avatar answered Oct 19 '22 18:10

Alexander Trakhimenok