<s> import os <EOL> import time <EOL> try : <EOL> import threading as _threading <EOL> except ImportError : <EOL> import dummy_threading as _threading <EOL> class EntropyPool ( object ) : <EOL> def __init__ ( self , seed = None ) : <EOL> self . pool_index = <NUM_LIT:0> <EOL> self . digest = None <EOL> self . next_byte = <NUM_LIT:0> <EOL> self . lock = _threading . Lock ( ) <EOL> try : <EOL> import hashlib <EOL> self . hash = hashlib . sha1 ( ) <EOL> self . hash_len = <NUM_LIT:20> <EOL> except : <EOL> try : <EOL> import sha <EOL> self . hash = sha . new ( ) <EOL> self . hash_len = <NUM_LIT:20> <EOL> except : <EOL> import md5 <EOL> self . hash = md5 . new ( ) <EOL> self . hash_len = <NUM_LIT:16> <EOL> self . pool = '<STR_LIT>' * self . hash_len <EOL> if not seed is None : <EOL> self . stir ( seed ) <EOL> self . seeded = True <EOL> else : <EOL> self . seeded = False <EOL> def stir ( self , entropy , already_locked = False ) : <EOL> if not already_locked : <EOL> self . lock . acquire ( ) <EOL> try : <EOL> bytes = [ ord ( c ) for c in self . pool ] <EOL> for c in entropy : <EOL> if self . pool_index == self . hash_len : <EOL> self . pool_index = <NUM_LIT:0> <EOL> b = ord ( c ) & <NUM_LIT> <EOL> bytes [ self . pool_index ] ^= b <EOL> self . pool_index += <NUM_LIT:1> <EOL> self . pool = '<STR_LIT>' . join ( [ chr ( c ) for c in bytes ] ) <EOL> finally : <EOL> if not already_locked : <EOL> self . lock . release ( ) <EOL> def _maybe_seed ( self ) : <EOL> if not self . seeded : <EOL> try : <EOL> seed = os . urandom ( <NUM_LIT:16> ) <EOL> except : <EOL> try : <EOL> r = file ( '<STR_LIT>' , '<STR_LIT:r>' , <NUM_LIT:0> ) <EOL> try : <EOL> seed = r . read ( <NUM_LIT:16> ) <EOL> finally : <EOL> r . close ( ) <EOL> except : <EOL> seed = str ( time . time ( ) ) <EOL> self . seeded = True <EOL> self . stir ( seed , True ) <EOL> def random_8 ( self ) : <EOL> self . lock . acquire ( ) <EOL> self . _maybe_seed ( ) <EOL> try : <EOL> if self . digest is None or self . next_byte == self . hash_len : <EOL> self . hash . update ( self . pool ) <EOL> self . digest = self . hash . digest ( ) <EOL> self . stir ( self . digest , True ) <EOL> self . next_byte = <NUM_LIT:0> <EOL> value = ord ( self . digest [ self . next_byte ] ) <EOL> self . next_byte += <NUM_LIT:1> <EOL> finally : <EOL> self . lock . release ( ) <EOL> return value <EOL> def random_16 ( self ) : <EOL> return self . random_8 ( ) * <NUM_LIT> + self . random_8 ( ) <EOL> def random_32 ( self ) : <EOL> return self . random_16 ( ) * <NUM_LIT> + self . random_16 ( ) <EOL> def random_between ( self , first , last ) : <EOL> size = last - first + <NUM_LIT:1> <EOL> if size > <NUM_LIT> L : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> if size > <NUM_LIT> : <EOL> rand = self . random_32 <EOL> max = <NUM_LIT> L <EOL> elif size > <NUM_LIT> : <EOL> rand = self . random_16 <EOL> max = <NUM_LIT> <EOL> else : <EOL> rand = self . random_8 <EOL> max = <NUM_LIT:255> <EOL> return ( first + size * rand ( ) // ( max + <NUM_LIT:1> ) ) <EOL> pool = EntropyPool ( ) <EOL> def random_16 ( ) : <EOL> return pool . random_16 ( ) <EOL> def between ( first , last ) : <EOL> return pool . random_between ( first , last ) </s>
