indexing description: "Objects that Generates random numbers" author: "Florian Keusch, fkeusch@student.ethz.ch" date: "$Date$" revision: "$Revision$" class RANDOM_NUMBER_GENERATOR create make feature {NONE} -- Random Access rand: RANDOM -- random number gen timer: TIME -- time at initialization init_seed: INTEGER -- the init seed of the random number generator next_nr: INTEGER -- the number of the next random number feature -- init make is -- initializes the random number generator do -- create timer with system time create timer.make_now init_seed := timer.milli_second create rand.make rand.set_seed (init_seed) next_nr := 1 end remake (seed: INTEGER) is -- reinitialize with new seed do rand.make rand.set_seed(seed) next_nr := 1 end feature -- functions next_int_mod (n: INTEGER) : INTEGER is -- get the next random integer value mod n -- if n is 101 you get numbers from 0 to 100 -- NOTE: n should be prime to get good results -- for example n := 101 require n_not_void: n /= Void do result := rand.next_random (next_nr) \\ n next_nr := next_nr + 1 end next_int_mod_minus_1 (n:INTEGER) : INTEGER is -- the same as next_int_mode but without zero require n_not_void: n /= Void local i: INTEGER do i := rand.next_random (next_nr) \\ n next_nr := next_nr + 1 if i = 0 then result := next_int_mod_minus_1 (n) else result := i end end next_int: INTEGER is -- get the next random integer value do result := rand.next_random (next_nr) next_nr := next_nr + 1 end next_double: DOUBLE is -- get the next random double value -- normalized between 0 and 1 do result := rand.double_i_th (next_nr) next_nr := next_nr + 1 end next_real: REAL is -- get the next random real value -- normalized between 0 and 1 do result := rand.real_i_th (next_nr) next_nr := next_nr + 1 end next_bool: BOOLEAN is -- get random boolean value local tmp: REAL temp: INTEGER do tmp := rand.real_i_th (next_nr) temp := tmp.rounded result := (temp = 1) next_nr := next_nr + 1 end end -- class RANDOM_NUMBER_GENERATOR