<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function <EOL> from random import Random as _Random <EOL> import math <EOL> import os <EOL> from tls . c import api <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> EPSILON = <NUM_LIT> <EOL> class RandomError ( EnvironmentError ) : <EOL> "<STR_LIT>" <EOL> class Random ( _Random ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , state = None ) : <EOL> """<STR_LIT>""" <EOL> self . seed ( state ) <EOL> self . gauss_next = None <EOL> def _rand_bytes ( self , buff , blen ) : <EOL> return api . RAND_bytes ( buff , blen ) <EOL> def random ( self ) : <EOL> """<STR_LIT>""" <EOL> buff = api . new ( '<STR_LIT>' ) <EOL> bptr = api . cast ( '<STR_LIT>' , buff ) <EOL> if not self . _rand_bytes ( bptr , len ( buff ) ) : <EOL> raise RandomError ( '<STR_LIT>' ) <EOL> num = <NUM_LIT:0> <EOL> for byte in buff : <EOL> num <<= <NUM_LIT:8> <EOL> num |= byte <EOL> num = ( num >> <NUM_LIT:3> ) * EPSILON <EOL> return num <EOL> def getrandbits ( self , bits ) : <EOL> """<STR_LIT>""" <EOL> bytes = int ( math . ceil ( bits / <NUM_LIT:8> ) ) <EOL> shift = abs ( <NUM_LIT:8> - ( bits % <NUM_LIT:8> ) ) % <NUM_LIT:8> <EOL> buff = api . new ( '<STR_LIT>' , bytes ) <EOL> bptr = api . cast ( '<STR_LIT>' , buff ) <EOL> if not self . _rand_bytes ( bptr , len ( buff ) ) : <EOL> raise RandomError ( '<STR_LIT>' ) <EOL> num = self . _seq_to_int ( buff ) >> shift <EOL> return num <EOL> def getrandbytes ( self , length ) : <EOL> "<STR_LIT>" <EOL> buff = api . new ( '<STR_LIT>' , length ) <EOL> self . _rand_bytes ( buff , length ) <EOL> return api . buffer ( buff ) [ : ] <EOL> def seed ( self , state , version = <NUM_LIT:2> , entropy = None ) : <EOL> """<STR_LIT>""" <EOL> if state is None : <EOL> if not api . RAND_status ( ) : <EOL> while not api . RAND_status ( ) : <EOL> data = api . new ( '<STR_LIT>' , os . urandom ( <NUM_LIT> ) ) <EOL> api . RAND_seed ( data , len ( data ) ) <EOL> return <EOL> elif isinstance ( state , ( str , bytes , bytearray ) ) : <EOL> if version > <NUM_LIT:1> : <EOL> if isinstance ( state , str ) : <EOL> state = state . encode ( ) <EOL> data = api . new ( '<STR_LIT>' , state ) <EOL> else : <EOL> state = hash ( state ) <EOL> data = self . _int_to_ubyte ( state ) <EOL> else : <EOL> data = self . _int_to_ubyte ( state ) <EOL> entropy = entropy if entropy is not None else <NUM_LIT:8> * len ( data ) <EOL> api . RAND_add ( data , len ( data ) , entropy ) <EOL> def _seq_to_int ( self , seq ) : <EOL> "<STR_LIT>" <EOL> num = <NUM_LIT:0> <EOL> for val in seq : <EOL> num = ( num << <NUM_LIT:8> ) | val <EOL> return num <EOL> def _int_to_ubyte ( self , num ) : <EOL> "<STR_LIT>" <EOL> if hasattr ( num , '<STR_LIT>' ) : <EOL> bit_len = num . bit_length ( ) <EOL> else : <EOL> value = num <EOL> bit_len = <NUM_LIT:0> <EOL> while value : <EOL> value >>= <NUM_LIT:1> <EOL> bit_len += <NUM_LIT:1> <EOL> bytes = int ( math . ceil ( bit_len / <NUM_LIT> ) ) <EOL> data = api . new ( '<STR_LIT>' , bytes ) <EOL> for pos in range ( bytes ) : <EOL> data [ pos ] = num & <NUM_LIT> <EOL> num = num >> <NUM_LIT:8> <EOL> return data <EOL> def _notimplemented ( self , * args , ** kwds ) : <EOL> "<STR_LIT>" <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> getstate = setstate = _notimplemented <EOL> class PseudoRandom ( Random ) : <EOL> """<STR_LIT>""" <EOL> def _rand_bytes ( self , buff , blen ) : <EOL> return api . RAND_pseudo_bytes ( buff , blen ) <EOL> _inst = Random ( ) <EOL> seed = _inst . seed <EOL> random = _inst . random <EOL> uniform = _inst . uniform <EOL> triangular = _inst . triangular <EOL> randint = _inst . randint <EOL> choice = _inst . choice <EOL> randrange = _inst . randrange <EOL> sample = _inst . sample <EOL> shuffle = _inst . shuffle <EOL> normalvariate = _inst . normalvariate <EOL> lognormvariate = _inst . lognormvariate <EOL> expovariate = _inst . expovariate <EOL> vonmisesvariate = _inst . vonmisesvariate <EOL> gammavariate = _inst . gammavariate <EOL> gauss = _inst . gauss <EOL> betavariate = _inst . betavariate <EOL> paretovariate = _inst . paretovariate <EOL> weibullvariate = _inst . weibullvariate <EOL> getstate = _inst . getstate <EOL> setstate = _inst . setstate <EOL> getrandbits = _inst . getrandbits <EOL> getrandbytes = _inst . getrandbytes </s>
