Advertisement
rfmonk

random_state.py

Jan 23rd, 2014
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.69 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3.  
  4. import random
  5. import os
  6. import cPickle as pickle
  7.  
  8. if os.path.exists('state.dat'):
  9.     # Restore the previously saved state
  10.     print 'Found state,dat, initializing random module'
  11.     with open('state.dat', 'rb') as f:
  12.         state = pickle.load(f)
  13.     random.setstate(state)
  14. else:
  15.     # Use a well-known start state
  16.     print 'No state.dat, seeding'
  17.     random.seed(1)
  18.  
  19. # Produce random values
  20. for i in xrange(3):
  21.     print '%04.3f' % random.random(),
  22. print
  23.  
  24. # Save the state for next time
  25. with open('state.dat', 'wb') as f:
  26.     pickle.dump(random.getstate(), f)
  27.  
  28. # Produce more random values
  29. print '\nAfter saving state:'
  30. for i in xrange(3):
  31.     print '%04.3f' % random.random()
  32. print
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement