<s> import os <EOL> import time <EOL> from rpython . rlib . rarithmetic import intmask <EOL> from rpython . rlib . rrandom import Random <EOL> from topaz . module import ClassDef <EOL> from topaz . objects . objectobject import W_Object <EOL> from topaz . objects . rangeobject import W_RangeObject <EOL> from topaz . coerce import Coerce <EOL> class W_RandomObject ( W_Object ) : <EOL> classdef = ClassDef ( "<STR_LIT>" , W_Object . classdef ) <EOL> def __init__ ( self , space , seed = <NUM_LIT:0> , klass = None ) : <EOL> W_Object . __init__ ( self , space , klass ) <EOL> self . w_seed = None <EOL> self . random = Random ( abs ( seed ) ) <EOL> @ classdef . setup_class <EOL> def setup_class ( cls , space , w_cls ) : <EOL> default = space . send ( w_cls , "<STR_LIT>" ) <EOL> space . set_const ( w_cls , "<STR_LIT>" , default ) <EOL> @ classdef . singleton_method ( "<STR_LIT>" ) <EOL> def method_allocate ( self , space ) : <EOL> return W_RandomObject ( space , <NUM_LIT:0> , self ) <EOL> @ classdef . method ( "<STR_LIT>" ) <EOL> def method_initialize ( self , space , w_seed = None ) : <EOL> self . srand ( space , w_seed ) <EOL> @ classdef . method ( "<STR_LIT>" ) <EOL> def method_seed ( self , space ) : <EOL> return self . w_seed <EOL> def srand ( self , space , seed = None ) : <EOL> previous_seed = self . w_seed <EOL> if seed is None : <EOL> seed = intmask ( self . _generate_seed ( ) ) <EOL> else : <EOL> seed = Coerce . int ( space , seed ) <EOL> self . w_seed = space . newint ( seed ) <EOL> if previous_seed is None : <EOL> value = space . newfloat ( self . random . random ( ) ) <EOL> else : <EOL> value = previous_seed <EOL> self . random = Random ( abs ( seed ) ) <EOL> return value <EOL> @ classdef . method ( "<STR_LIT>" ) <EOL> def method_rand ( self , space , w_max = None ) : <EOL> if w_max is None : <EOL> return space . newfloat ( self . random . random ( ) ) <EOL> elif space . is_kind_of ( w_max , space . w_float ) : <EOL> return self . _rand_float ( space , w_max ) <EOL> elif space . is_kind_of ( w_max , space . getclassfor ( W_RangeObject ) ) : <EOL> return self . _rand_range ( space , w_max ) <EOL> else : <EOL> return self . _rand_int ( space , w_max ) <EOL> @ classdef . singleton_method ( "<STR_LIT>" ) <EOL> def method_singleton_rand ( self , space , args_w ) : <EOL> default = space . find_const ( self , "<STR_LIT>" ) <EOL> return space . send ( default , "<STR_LIT>" , args_w ) <EOL> def _rand_range ( self , space , range ) : <EOL> random = self . random . random ( ) <EOL> first = space . send ( range , "<STR_LIT>" ) <EOL> last = space . send ( range , "<STR_LIT>" ) <EOL> if space . is_true ( space . send ( range , "<STR_LIT>" , [ last ] ) ) : <EOL> last = space . send ( last , "<STR_LIT:+>" , [ space . newint ( <NUM_LIT:1> ) ] ) <EOL> diff = space . send ( last , "<STR_LIT:->" , [ first ] ) <EOL> offset = space . send ( diff , "<STR_LIT:*>" , [ space . newfloat ( random ) ] ) <EOL> choice = space . send ( offset , "<STR_LIT:+>" , [ first ] ) <EOL> if ( not space . is_kind_of ( first , space . w_float ) and <EOL> not space . is_kind_of ( last , space . w_float ) ) : <EOL> choice = space . send ( choice , "<STR_LIT>" ) <EOL> return choice <EOL> def _rand_float ( self , space , float ) : <EOL> random = self . random . random ( ) <EOL> max = Coerce . float ( space , float ) <EOL> if max <= <NUM_LIT:0> : <EOL> raise space . error ( space . w_ArgumentError , "<STR_LIT>" ) <EOL> return space . newfloat ( random * max ) <EOL> def _rand_int ( self , space , integer ) : <EOL> random = self . random . random ( ) <EOL> max = Coerce . int ( space , integer ) <EOL> if max <= <NUM_LIT:0> : <EOL> raise space . error ( space . w_ArgumentError , "<STR_LIT>" ) <EOL> else : <EOL> return space . newint ( int ( random * max ) ) <EOL> def _generate_seed ( self ) : <EOL> seed = <NUM_LIT:0> <EOL> if os . access ( '<STR_LIT>' , os . R_OK ) : <EOL> file = os . open ( '<STR_LIT>' , os . R_OK , <NUM_LIT:0> <NUM_LIT> ) <EOL> bytes = os . read ( file , <NUM_LIT:4> ) <EOL> os . close ( file ) <EOL> for b in bytes : <EOL> seed = seed * <NUM_LIT> + ord ( b ) <EOL> return seed + int ( time . time ( ) ) + os . getpid ( ) </s>
