Random generator always returns same value on every run

5,346 views
Skip to first unread message

Alex Vizor

unread,
Apr 2, 2012, 11:04:22 AM4/2/12
to golang-nuts
Hello guys,

I observe strange behavior. Seems pseudo random generator is indeed
very pseudo. It generates same set of values on every program run. Try
following:
http://play.golang.org/p/3Syxa7YJr9

Run this code several times and you will be same sequence of values
every time, I think this is not normal. Is this a bug?

andrey mirtchovski

unread,
Apr 2, 2012, 11:06:34 AM4/2/12
to Alex Vizor, golang-nuts
"If Seed is not called, the generator behaves as if seeded by Seed(1)."

http://golang.org/pkg/math/rand/#Seed

Alex Vizor

unread,
Apr 2, 2012, 11:10:04 AM4/2/12
to golang-nuts
Ah, thanks, my bad. But why this isn't mentioned in doc header for
this package? I thought that it does this for me.

andrey mirtchovski

unread,
Apr 2, 2012, 11:40:06 AM4/2/12
to Alex Vizor, golang-nuts
> Ah, thanks, my bad. But why this isn't mentioned in doc header for
> this package? I thought that it does this for me.

probably because no default seed suitable for most purposes has been
found (and 1 is as good a guess as any). crypto/rand probably behaves
closer to what you expect, but it's a completely different mechanism
best used sparingly. it can give you a good seed though, when you need
one.

minux

unread,
Apr 2, 2012, 11:43:53 AM4/2/12
to andrey mirtchovski, Alex Vizor, golang-nuts
I think the main reason for a default seed is reproducibility. When you're debugging
your random related algorithm if every run has different behavior, it will be very hard
to diagnosis the problem. After you've debugged your code, you can safely select
a more appropriate seed. Also, the proper seed is different for different kind of program,
so the math/rand package doesn't try to select one for you.

André Moraes

unread,
Apr 2, 2012, 12:11:53 PM4/2/12
to Alex Vizor, golang-nuts

Just to add another piece of info:

play.golang.org caches the result of runs, so if you type the same
program twice there is a possibility that the response will be fetched
from cache instead of being compiled and runned.

So even if you changed the Seed you could observe the same results
(when running on the play.golang.org).

To make sure that your are with a fresh result, change something
before running or run the code on your computer.

--
André Moraes
http://andredevchannel.blogspot.com/

Kyle Lemons

unread,
Apr 2, 2012, 1:24:47 PM4/2/12
to André Moraes, Alex Vizor, golang-nuts
2012/4/2 André Moraes <and...@gmail.com>

>
> I observe strange behavior. Seems pseudo random generator is indeed
> very pseudo. It generates same set of values on every program run. Try
> following:
> http://play.golang.org/p/3Syxa7YJr9
>
> Run this code several times and you will be same sequence of values
> every time, I think this is not normal. Is this a bug?

Just to add another piece of info:

play.golang.org caches the result of runs, so if you type the same
program twice there is a possibility that the response will be fetched
from cache instead of being compiled and runned.

So even if you changed the Seed you could observe the same results
(when running on the play.golang.org).

You can't seed with time either, because the playground is frozen in time.  It was probably designed so that programs always have a deterministic result.

Carlos Cobo

unread,
Apr 2, 2012, 3:32:17 PM4/2/12
to golan...@googlegroups.com
play.golang isn't a good place to try things with random numbers.

Because its caching like André mentioned and also because in there it's always the same date: 2009-11-10 23:00:00 +0000 UTC

hs

unread,
May 16, 2012, 1:32:44 PM5/16/12
to golang-nuts
On Apr 2, 11:43 am, minux <minux...@gmail.com> wrote:
> I think the main reason for a default seed is reproducibility.

I need exactly this: Random numbers which will be the same across all
supported platforms.

Will I achieve this by using a fixed seed?

Regards,

Heiko

minux

unread,
May 16, 2012, 1:42:39 PM5/16/12
to hs, golang-nuts
Yes. But you don't need to do this yourself, as the default random source for
math/rand uses 1 as initial seed.

Brendan Tracey

unread,
Feb 7, 2013, 6:59:29 PM2/7/13
to golan...@googlegroups.com, hs
Is there an idiomatic way to set the random number seed if we want a different one for every one (some specific call to the time or something)? I realize there are different ways one could do it, and there are sometimes specific considerations, but I'm just wondering if there is a "standard" way to do it

Scott Pakin

unread,
Feb 7, 2013, 8:37:52 PM2/7/13
to golan...@googlegroups.com

Russ Cox

unread,
Feb 7, 2013, 11:34:27 PM2/7/13
to Brendan Tracey, golang-nuts, hs
On Thu, Feb 7, 2013 at 6:59 PM, Brendan Tracey <tracey....@gmail.com> wrote:
Is there an idiomatic way to set the random number seed if we want a different one for every one (some specific call to the time or something)? I realize there are different ways one could do it, and there are sometimes specific considerations, but I'm just wondering if there is a "standard" way to do it

rand.Seed(time.Now().UnixNano()) is as good as anything else.

John Nagle

unread,
Feb 8, 2013, 2:27:41 PM2/8/13
to golan...@googlegroups.com
On 2/7/2013 5:37 PM, Scott Pakin wrote:
> On Monday, April 2, 2012 9:04:22 AM UTC-6, Alex wrote:
>
>> I observe strange behavior. Seems pseudo random generator is indeed
>> very pseudo. It generates same set of values on every program run. Try
>> following:
>> http://play.golang.org/p/3Syxa7YJr9
>>
>> Run this code several times and you will be same sequence of values
>> every time, I think this is not normal. Is this a bug?

It's a feature of the Go Playground. The start time for
all Playground programs is the same, to make the Playground
deterministic.

John Nagle

Reply all
Reply to author
Forward
0 new messages