[IPython-dev] Re: [FWD] An interesting take on the Notebook Problem

Robert Kern rkern at ucsd.edu
Mon Aug 1 07:58:39 CDT 2005


Hans Meine wrote:
> On Friday 22 July 2005 16:05, Toni Alatalo wrote:
> 
>>[...] quick Googling now didn't tell me if the Python
>>random generator is guaranteed to give the same results with the same
>>seed and in what conditions (i know from working on procedural modelling
>>that it at least works on the same computer..), but that can be looked
>>at later.
> 
> Confusingly, calling "random.seed(...)" is not enough.  A too quick check once 
> gave me the impression that it was (the first call of random.random() indeed 
> returned the same value), but in order to store/restore the complete state 
> you need to use the provided functions random.getstate() and 
> random.setstate() which return the state as a a 3-tuple of an int, a tuple 
> containing 625 ints and a None here.

random.seed() deterministically sets the whole state from an 
integer/long. Calling random.seed() should be enough.

In [1]: import random

In [2]: random.seed(123456789)

In [3]: [random.random() for i in range(20)]
Out[3]:
[0.64140061618587263,
  0.54218926809694945,
  0.99317506628327212,
  0.84325213668691656,
  0.81173392833794056,
  0.3971737100780004,
  0.93709510791204254,
  0.6891026531658162,
  0.39711048852598374,
  0.35102519242304475,
  0.399603131718545,
  0.54558174824433847,
  0.207714227402927,
  0.6001575513874905,
  0.023680264289157371,
  0.61140144008940833,
  0.026211036291071466,
  0.62015590923237174,
  0.73967307701763618,
  0.31412119286874374]

In [4]: random.seed(123456789)

In [5]: [random.random() for i in range(20)]
Out[5]:
[0.64140061618587263,
  0.54218926809694945,
  0.99317506628327212,
  0.84325213668691656,
  0.81173392833794056,
  0.3971737100780004,
  0.93709510791204254,
  0.6891026531658162,
  0.39711048852598374,
  0.35102519242304475,
  0.399603131718545,
  0.54558174824433847,
  0.207714227402927,
  0.6001575513874905,
  0.023680264289157371,
  0.61140144008940833,
  0.026211036291071466,
  0.62015590923237174,
  0.73967307701763618,
  0.31412119286874374]

-- 
Robert Kern
rkern at ucsd.edu

"In the fields of hell where the grass grows high
  Are the graves of dreams allowed to die."
   -- Richard Harter




More information about the IPython-dev mailing list