Random randomness

eric jones eric at enthought.com
Wed Jan 23 17:58:17 EST 2002


Hey Jarrett,

The calculations done in random.gauss produces random numbers in pairs.  It
caches one of them and returns the other.  The next time gauss is called, it
checks to see if it has a cached value.  If so, it returns this with doing
another calculation.  The random.seed function doesn't clear this cache, so
your seeing the left over value from the previous calculation.  To make sure
that you get a the same number from gauss, you can do the following:


>>> import random
>>> random.seed(10)
>>> random.gauss(0,1)
1.1405071414724208
>>> random.seed(10)
>>> random._inst.gauss_next = None
>>> random.gauss(0,1)
1.1405071414724208

hope that helps,

eric


"W. Jarrett Campbell" <jarrett at engineer.com> wrote in message
news:dYG38.10497$ZK4.420935 at typhoon.austin.rr.com...
> I'm working on a project that requires a random number generated with the
> same value each time the program is run but I'm seeing some behavior I
> cannot explain.  Could someone explain to me why the following code does
not
> result in two random numbers where a = b?
>
> FYI, I'm running Python 2.1 on win32s...
>
> C:\>python
> ActivePython 2.1, build 210 ActiveState)
> based on Python 2.1 (#15, Apr 19 2001, 10:28:27) [MSC 32 bit (Intel)] on
> win32
> Type "help", "copyright", "credits" or "license" for more information.
> >>> import random
> >>>
> >>> random.seed(10)
> >>> a=random.gauss(0,1)
> >>> random.seed(10)
> >>> b=random.gauss(0,1)
> >>> print a
> 1.14050714147
> >>> print b
> 0.566965824829
>
>
> Thanks in advance,
>
> Jarrett Campbell
> jarrett at engineer.com
>
>





More information about the Python-list mailing list