[code.view]

[top] / python / PyMOTW / random / random_state.py

     #!/usr/bin/env python
     # encoding: utf-8
     #
     # Copyright (c) 2010 Doug Hellmann.  All rights reserved.
     #
     """Save and restore state
     """
     #end_pymotw_header
     
     import random
     import os
     import cPickle as pickle
     
     if os.path.exists('state.dat'):
         # Restore the previously saved sate
         print 'Found state.dat, initializing random module'
         with open('state.dat', 'rb') as f:
             state = pickle.load(f)
         random.setstate(state)
     else:
         # Use a well-known start state
         print 'No state.dat, seeding'
         random.seed(1)
     
     # Produce random values
     for i in xrange(3):
         print '%04.3f' % random.random()
     
     # Save state for next time
     with open('state.dat', 'wb') as f:
         pickle.dump(random.getstate(), f)
     
     # Produce more random values
     print '\nAfter saving state:'
     for i in xrange(3):
         print '%04.3f' % random.random()
     

[top] / python / PyMOTW / random / random_state.py

contact | logmethods.com