Automatic generation produced by ISE Eiffel

Classes Clusters Cluster hierarchy Chart Relations Text Flat Contracts Flat contracts Go to:
indexing description: "Pseudo-random number sequence, linear congruential method" status: "See notice at end of class" names: random date: "$Date: 2001-11-16 20:32:23 +0000 (Fri, 16 Nov 2001) $" revision: "$Revision: 51435 $" class RANDOM create make, set_seed feature -- Initialization make is -- Initialize structure using a default seed. do set_seed (default_seed) ensure seed_set: seed = default_seed end set_seed (s: INTEGER) is -- Initialize sequence using s as the seed. require non_negative: s >= 0 do seed := s last_result := seed last_item := 0 ensure seed_set: seed = s end feature -- Access default_seed: INTEGER is -- Default value 123,457; -- may be redefined for a new generator. once Result := 123457 end double_i_th (i: INTEGER): DOUBLE is -- The i-th random number as a double between 0 and 1 local d: DOUBLE do d := i_th (i) Result := d / dmod end double_item: DOUBLE is -- The current random number as a double between 0 and 1 local d: DOUBLE do d := item Result := d / dmod end has (n: INTEGER): BOOLEAN is -- Will n be part of the random number sequence? do Result := (n < modulus) and (n >= 0) ensure -- from CONTAINER not_found_in_empty: Result implies not is_empty ensure then only_: Result = (n < modulus and n >= 0) end i_th (i: INTEGER): INTEGER is -- The i-th random number require -- from COUNTABLE positive_argument: i > 0 local count: INTEGER do if i >= last_item then Result := last_result count := last_item else Result := seed end from until count = i loop Result := randomize (Result) count := count + 1 end last_result := Result last_item := i ensure then in_range: (Result < modulus) and (Result >= 0) end increment: INTEGER is -- Default value 0; -- may be redefined for a new generator. once Result := 0 end index: INTEGER -- Index of current position -- (from COUNTABLE_SEQUENCE) index_of (v: like item; i: INTEGER): INTEGER is -- Index of i-th occurrence of v. -- 0 if none. -- (Reference or object equality, -- based on object_comparison.) -- (from LINEAR) require -- from LINEAR positive_occurrences: i > 0 local occur, pos: INTEGER do if object_comparison and v /= void then from start pos := 1 until off or (occur = i) loop if item /= void and then v.is_equal (item) then occur := occur + 1 end forth pos := pos + 1 end else from start pos := 1 until off or (occur = i) loop if item = v then occur := occur + 1 end forth pos := pos + 1 end end if occur = i then Result := pos - 1 end ensure -- from LINEAR non_negative_result: Result >= 0 end item: INTEGER is -- Item at current position -- (from COUNTABLE_SEQUENCE) require -- from TRAVERSABLE not_off: not off require -- from ACTIVE readable: readable do Result := i_th (index) end modulus: INTEGER is -- Default value 2^31 -1 = 2,147,483,647; -- may be redefined for a new generator. once Result := 2147483647 end multiplier: INTEGER is -- Default value 7^5 = 16,807; -- may be redefined for a new generator. once Result := 16807 end next_random (n: INTEGER): INTEGER is -- Next random number after n -- in pseudo-random order require in_range: (n < modulus) and (n >= 0) do Result := randomize (n) ensure in_range: (Result < modulus) and (Result >= 0) end occurrences (v: INTEGER): INTEGER is -- Number of times v appears. -- (Reference or object equality, -- based on object_comparison.) -- (from LINEAR) do from start search (v) until exhausted loop Result := Result + 1 forth search (v) end ensure -- from BAG non_negative_occurrences: Result >= 0 end real_i_th (i: INTEGER): REAL is -- The i-th random number as a real between 0 and 1 local r1, r2: REAL do r1 := i_th (i) r2 := modulus Result := r1 / r2 end real_item: REAL is -- The current random number as a real between 0 and 1 local r1, r2: REAL do r1 := item r2 := modulus Result := r1 / r2 end search (v: like item) is -- Move to first position (at or after current -- position) where item and v are equal. -- (Reference or object equality, -- based on object_comparison.) -- If no such position ensure that exhausted will be true. -- (from LINEAR) do if object_comparison and v /= void then from until exhausted or else (item /= void and then v.is_equal (item)) loop forth end else from until exhausted or else v = item loop forth end end ensure -- from LINEAR object_found: (not exhausted and object_comparison) implies equal (v, item) item_found: (not exhausted and not object_comparison) implies v = item end seed: INTEGER -- Seed for sequence. feature {NONE} -- Access arc_cosine (v: DOUBLE): DOUBLE is -- Trigonometric arccosine of radian v -- in the range [0, pi] -- (from DOUBLE_MATH) external "C | <math.h>" alias "acos" end arc_sine (v: DOUBLE): DOUBLE is -- Trigonometric arcsine of radian v -- in the range [-pi/2, +pi/2] -- (from DOUBLE_MATH) external "C | <math.h>" alias "asin" end arc_tangent (v: DOUBLE): DOUBLE is -- Trigonometric arctangent of radian v -- in the range [-pi/2, +pi/2] -- (from DOUBLE_MATH) external "C | <math.h>" alias "atan" end ceiling (v: DOUBLE): DOUBLE is -- Least integral greater than or equal to v -- (from DOUBLE_MATH) external "C | <math.h>" alias "ceil" end cosine (v: DOUBLE): DOUBLE is -- Trigonometric cosine of radian v approximated -- in the range [-pi/4, +pi/4] -- (from DOUBLE_MATH) external "C | <math.h>" alias "cos" end dabs (v: DOUBLE): DOUBLE is -- Absolute of v -- (from DOUBLE_MATH) external "C | <math.h>" alias "fabs" end Euler: DOUBLE is 2.7182818284590452354 -- Logarithm base -- (from MATH_CONST) exp (x: DOUBLE): DOUBLE is -- Exponential of v. -- (from DOUBLE_MATH) external "C | <math.h>" end floor (v: DOUBLE): DOUBLE is -- Greatest integral less than or equal to v -- (from DOUBLE_MATH) external "C | <math.h>" end log (v: DOUBLE): DOUBLE is -- Natural logarithm of v -- (from DOUBLE_MATH) external "C | <math.h>" end log10 (v: DOUBLE): DOUBLE is -- Base 10 logarithm of v -- (from DOUBLE_MATH) external "C | <math.h>" end log_2 (v: DOUBLE): DOUBLE is -- Base 2 logarithm of v -- (from DOUBLE_MATH) local a: DOUBLE do a := 2.0 Result := log (v) / log (a) end Pi: DOUBLE is 3.14159265358979323846 -- (from MATH_CONST) sine (v: DOUBLE): DOUBLE is -- Trigonometric sine of radian v approximated -- in range [-pi/4, +pi/4] -- (from DOUBLE_MATH) external "C | <math.h>" alias "sin" end sqrt (v: DOUBLE): DOUBLE is -- Square root of v -- (from DOUBLE_MATH) external "C | <math.h>" end Sqrt2: DOUBLE is 1.41421356237309504880 -- Square root of 2 -- (from MATH_CONST) tangent (v: DOUBLE): DOUBLE is -- Trigonometric tangent of radian v approximated -- in range [-pi/4, +pi/4] -- (from DOUBLE_MATH) external "C | <math.h>" alias "tan" end feature -- Status report After: BOOLEAN is False -- Is current position past last item? (Answer: no.) -- (from COUNTABLE_SEQUENCE) changeable_comparison_criterion: BOOLEAN is -- May object_comparison be changed? -- (Answer: yes by default.) -- (from CONTAINER) do Result := True end empty: BOOLEAN is obsolete "ELKS 2000: Use `is_empty' instead" -- Is there no element? -- (from CONTAINER) do Result := is_empty end exhausted: BOOLEAN is -- Has structure been completely explored? -- (from LINEAR) do Result := off ensure -- from LINEAR exhausted_when_off: off implies Result end Extendible: BOOLEAN is False -- May items be added? (Answer: no.) -- (from COUNTABLE_SEQUENCE) Full: BOOLEAN is True -- The structure is complete -- (from INFINITE) Is_empty: BOOLEAN is False -- Is structure empty? (Answer: no.) -- (from INFINITE) is_inserted (v: INTEGER): BOOLEAN is -- Has v been inserted by the most recent insertion? -- (By default, the value returned is equivalent to calling -- `has (v)'. However, descendants might be able to provide more -- efficient implementations.) -- (from COLLECTION) do Result := has (v) end object_comparison: BOOLEAN -- Must search operations use equal rather than = -- for comparing references? (Default: no, use =.) -- (from CONTAINER) off: BOOLEAN is -- Is there no current item? -- (from LINEAR) do Result := is_empty or after end Prunable: BOOLEAN is False -- May items be removed? (Answer: no.) -- (from COUNTABLE_SEQUENCE) Readable: BOOLEAN is True -- Is there a current item that may be read? -- (Answer: yes.) -- (from COUNTABLE_SEQUENCE) Writable: BOOLEAN is False -- Is there a current item that may be written? -- (Answer: no.) -- (from COUNTABLE_SEQUENCE) feature -- Status setting compare_objects is -- Ensure that future search operations will use equal -- rather than = for comparing references. -- (from CONTAINER) require -- from CONTAINER changeable_comparison_criterion do object_comparison := True ensure -- from CONTAINER object_comparison end compare_references is -- Ensure that future search operations will use = -- rather than equal for comparing references. -- (from CONTAINER) require -- from CONTAINER changeable_comparison_criterion do object_comparison := False ensure -- from CONTAINER reference_comparison: not object_comparison end feature -- Cursor movement forth is -- Move to next position. -- (from COUNTABLE_SEQUENCE) require -- from LINEAR not_after: not after do index := index + 1 end start is -- Move to first position. -- (from COUNTABLE_SEQUENCE) do index := 1 end feature {NONE} -- Element change fill (other: CONTAINER [INTEGER]) is -- Fill with as many items of other as possible. -- The representations of other and current structure -- need not be the same. -- (from COLLECTION) require -- from COLLECTION other_not_void: other /= void extendible local lin_rep: LINEAR [INTEGER] do lin_rep := other.linear_representation from lin_rep.start until not extendible or else lin_rep.off loop extend (lin_rep.item) lin_rep.forth end end feature {NONE} -- Removal prune_all (v: INTEGER) is -- Remove all occurrences of v. -- (Reference or object equality, -- based on object_comparison.) -- (from COLLECTION) require -- from COLLECTION prunable do from until not has (v) loop prune (v) end ensure -- from COLLECTION no_more_occurrences: not has (v) end feature {NONE} -- Inapplicable extend (v: INTEGER) is -- Add v at end. -- (from COUNTABLE_SEQUENCE) require -- from COLLECTION extendible: extendible do ensure -- from COLLECTION item_inserted: is_inserted (v) ensure then -- from BAG one_more_occurrence: occurrences (v) = old (occurrences (v)) + 1 end finish is -- Move to last position. -- (from COUNTABLE_SEQUENCE) do ensure then -- from COUNTABLE_SEQUENCE failure: False end linear_representation: LINEAR [INTEGER] is -- Representation as a linear structure -- (from COUNTABLE_SEQUENCE) do end prune (v: INTEGER) is -- Remove first occurrence of v, if any. -- (from COUNTABLE_SEQUENCE) require -- from COLLECTION prunable: prunable do end put (v: INTEGER) is -- Add v to the right of current position. -- (from COUNTABLE_SEQUENCE) require -- from COLLECTION extendible: extendible do ensure -- from COLLECTION item_inserted: is_inserted (v) end remove is -- Remove item to the right of current position. -- (from COUNTABLE_SEQUENCE) require -- from ACTIVE prunable: prunable writable: writable do end replace (v: INTEGER) is -- Replace by v item at current position. -- (from COUNTABLE_SEQUENCE) require -- from ACTIVE writable: writable do ensure -- from ACTIVE item_replaced: item = v end wipe_out is -- Remove all items. -- (from COUNTABLE_SEQUENCE) require -- from COLLECTION prunable do ensure -- from COLLECTION wiped_out: is_empty end feature {NONE} -- Implementation dinc: DOUBLE is -- Double value for increment once Result := increment end dmod: DOUBLE is -- Double value for modulus once Result := modulus end dmul: DOUBLE is -- Double value for multiplier once Result := multiplier end double_mod (x, m: DOUBLE): DOUBLE is -- x modulo m do Result := x - (floor (x / m) * m) end last_item: INTEGER -- Last item requested last_result: INTEGER -- Value from last call to item randomize (xn: INTEGER): INTEGER is -- Next item local x: DOUBLE do x := double_mod (dmul * xn + dinc, dmod) Result := x.truncated_to_integer end feature -- Iteration do_all (action: PROCEDURE [ANY, TUPLE [INTEGER]]) is -- Apply action to every item. -- Semantics not guaranteed if action changes the structure; -- in such a case, apply iterator to clone of structure instead. -- (from LINEAR) require -- from TRAVERSABLE action_exists: action /= void local t: TUPLE [INTEGER] do create t.make from start until after loop t.put (item, 1) action.call (t) forth end end do_if (action: PROCEDURE [ANY, TUPLE [INTEGER]]; test: FUNCTION [ANY, TUPLE [INTEGER], BOOLEAN]) is -- Apply action to every item that satisfies test. -- Semantics not guaranteed if action or test changes the structure; -- in such a case, apply iterator to clone of structure instead. -- (from LINEAR) require -- from TRAVERSABLE action_exists: action /= void test_exits: test /= void local t: TUPLE [INTEGER] do create t.make from start until after loop t.put (item, 1) if test.item (t) then action.call (t) end forth end end for_all (test: FUNCTION [ANY, TUPLE [INTEGER], BOOLEAN]): BOOLEAN is -- Is test true for all items? -- (from LINEAR) require -- from TRAVERSABLE test_exits: test /= void local cs: CURSOR_STRUCTURE [INTEGER] c: CURSOR t: TUPLE [INTEGER] do create t.make cs ?= Current if cs /= void then c := cs.cursor end from start Result := True until after or not Result loop t.put (item, 1) Result := test.item (t) forth end if cs /= void then cs.go_to (c) end end there_exists (test: FUNCTION [ANY, TUPLE [INTEGER], BOOLEAN]): BOOLEAN is -- Is test true for at least one item? -- (from LINEAR) require -- from TRAVERSABLE test_exits: test /= void local cs: CURSOR_STRUCTURE [INTEGER] c: CURSOR t: TUPLE [INTEGER] do create t.make cs ?= Current if cs /= void then c := cs.cursor end from start until after or Result loop t.put (item, 1) Result := test.item (t) forth end if cs /= void then cs.go_to (c) end end invariant non_negative_seed: seed >= 0 non_negative_increment: increment >= 0 positive_multiplier: multiplier > 0 modulus_constraint: modulus > 1 -- from ANY reflexive_equality: standard_is_equal (Current) reflexive_conformance: conforms_to (Current) -- from INFINITE never_empty: not is_empty always_full: full -- from ACTIVE writable_constraint: writable implies readable empty_constraint: is_empty implies (not readable) and (not writable) -- from LINEAR after_constraint: after implies off -- from TRAVERSABLE empty_constraint: is_empty implies off indexing library: "[ EiffelBase: Library of reusable components for Eiffel. ]" status: "[ Copyright 1986-2001 Interactive Software Engineering (ISE). For ISE customers the original versions are an ISE product covered by the ISE Eiffel license and support agreements. ]" license: "[ EiffelBase may now be used by anyone as FREE SOFTWARE to develop any product, public-domain or commercial, without payment to ISE, under the terms of the ISE Free Eiffel Library License (IFELL) at http://eiffel.com/products/base/license.html. ]" source: "[ Interactive Software Engineering Inc. ISE Building 360 Storke Road, Goleta, CA 93117 USA Telephone 805-685-1006, Fax 805-685-6869 Electronic mail <info@eiffel.com> Customer support http://support.eiffel.com ]" info: "[ For latest info see award-winning pages: http://eiffel.com ]" end -- class RANDOM
Classes Clusters Cluster hierarchy Chart Relations Text Flat Contracts Flat contracts Go to:

-- Generated by ISE Eiffel --
For more details: www.eiffel.com