File: std_rand.e

package info (click to toggle)
smarteiffel 1.1-11
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 12,288 kB
  • ctags: 40,785
  • sloc: ansic: 35,791; lisp: 4,036; sh: 1,783; java: 895; ruby: 613; python: 209; makefile: 115; csh: 78; cpp: 50
file content (97 lines) | stat: -rw-r--r-- 1,927 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
-- This file is  free  software, which  comes  along  with  SmartEiffel. This
-- software  is  distributed  in the hope that it will be useful, but WITHOUT 
-- ANY  WARRANTY;  without  even  the  implied warranty of MERCHANTABILITY or
-- FITNESS  FOR A PARTICULAR PURPOSE. You can modify it as you want, provided
-- this header is kept unaltered, and a notification of the changes is added.
-- You  are  allowed  to  redistribute  it and sell it, alone or as a part of 
-- another product.
--          Copyright (C) 1994-98 LORIA - UHP - CRIN - INRIA - FRANCE
--            Dominique COLNET and Suzanne COLLIN - colnet@loria.fr 
--                       http://www.loria.fr/SmartEiffel
--
class STD_RAND
   -- Press' standard generator, which uses the minimal standard 
   -- and then uses shuffling to break up short order corelations.
   
creation with_seed

feature {NONE}
   
   seed:INTEGER

   ia: INTEGER is 16807;

   im: INTEGER is 2147483647;

   iq: INTEGER is 127773;

   ir: INTEGER is 2836;

   iv: ARRAY[INTEGER];

   ntab: INTEGER is 32;

   iy: INTEGER;

feature {NONE}

   min_next is
      local
	 k: INTEGER;
      do
	 k := seed // iq;
	 seed := ia * (seed -k * iq) - ir * k;
	 if seed < 0 then
	    seed := seed + im;
	 end;
      end;

   with_seed(seed_value:INTEGER) is
      require
	 valid_seed: seed_value > 0 and seed_value < im
      local
	 i: INTEGER;
      do
	 seed := seed_value;
	 min_next;
	 !!iv.make(1,ntab);
	 from 
	    i := 1;
	 until 
	    i > 7
	 loop
	    min_next;
	    i := i + 1;
	 end;
	 from
	    i := ntab;
	 until
	    i = 0
	 loop
	    iv.put(seed,i);
	    min_next;
	    i := i - 1;
	 end
	 iy := iv.item(1);
	 next;
      end

feature

   next is
      local
	 tmp:INTEGER
      do
	 tmp := iy \\ ntab + 1;
	 min_next;
	 iy := iv.item(tmp);
	 iv.put(seed,tmp);
      end

   last_integer(n:INTEGER):INTEGER is
      do
	 Result := iy \\ n + 1;
      end

end -- STD_RAND