KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgap > util > randomX


1 package org.jgap.util;
2
3 /**
4     Abstract superclass for emulations of java.util.Random with
5     various underlying generators. These generators provide a
6     superset of the methods of the built-in Java generator, and
7     allow easy replacement of the low-level byte-stream random
8     generator without the need to reimplement the higher-level
9     calls.
10 <p>
11     The nature of the data returned by the functions in this class
12     depends upon the generator provided by the class derived from it.
13     If the generator is algorithmic, the data are pseudorandom; if a
14     hardware generator is employed, genuine random data may be
15     obtained. For brevity, in this document, we use <em>random</em>
16     to refer to the data returned, whatever its actual source.
17 <p>
18     Designed and implemented in July 1996 by
19     <a HREF="http://www.fourmilab.ch/">John Walker</a>,
20     <a HREF="mailto:kelvin@fourmilab.ch">kelvin@fourmilab.ch</a>.
21
22 @see java.util.Random
23 */

24
25 public abstract class randomX {
26
27     private int nbits = 0;
28     private boolean iset = false;
29
30     /** Reset when seed changes. A generator which supports seed
31         must call this method by <tt>super.setSeed()</tt> when its own
32         <tt>setSeed(</tt><i>long</i><tt>)</tt> method is called. This
33         allows randomX to discard any buffered data in the
34         <tt>nextBit()</tt> and <tt>nextGaussian()</tt> methods so that
35         subsequent calls will immediately reflect the new seed.
36
37         <p>
38         If a derived class does not permit specification of a seed
39         (hardware-based generators, for example), it should declare:
40
41         <p>
42         <blockquote>
43             <tt>private void setSeed(long seed) { }</tt>
44         </blockquote>
45         <p>
46         which will hide the setSeed method from its users and cause
47         a compile-time error if a program attempts to specify a seed. */

48
49     public void setSeed() {
50         nbits = 0;
51         iset = false;
52     }
53
54     /** Return next [pseudo]random byte from low level generator. All
55         generators derived from this class must implement
56         <tt>nextByte()</tt>. */

57
58     public abstract byte nextByte();
59
60     /* Emulation of standard java.util.Random methods. Given
61         an implementation of nextByte() in the derived class, these
62         methods provide all the other forms of results. A derived
63         class is, of course, free to reimplement any of these.
64         For example, a generator which produces 31 high-quality
65         pseudorandom bits on each call might reimplement nextBit(),
66         nextShort(), and nextFloat() to avoid the inefficiency of
67         passing individual bytes to the parent class, then reassembling
68         to return to the caller. */

69
70     /** @return the next random, uniformly distributed, <tt>int</tt>
71                 value. */

72
73     public int nextInt() {
74         return (int) ((((int) nextShort()) << 16) | (((int) nextShort()) & 0xFFFF));
75     }
76
77     /** @return the next random, uniformly distributed, <tt>long</tt>
78                 value. */

79
80     public long nextLong() {
81         return (long) ((((long) nextInt()) << 32) | (((long) nextInt()) & 0xFFFFFFFFl));
82     }
83
84     /** @return the next random, uniformly distributed, <tt>float</tt>
85                 value, greater than or equal to 0 and less than 1. */

86
87     public float nextFloat() {
88         return (float) ((nextInt() & 0x7FFFFFFF) / (0x7FFFFFFF * 1.0));
89     }
90
91     /** @return the next random, uniformly distributed, <tt>double</tt>
92                 value, greater than or equal to 0 and less than 1. */

93
94     public double nextDouble() {
95         return (double) ((nextLong() & 0x7FFFFFFFFFFFFFFFl) /
96                          (0x7FFFFFFFFFFFFFFFl * 1.0));
97     }
98
99     private double gset;
100
101     /** @return the next Gaussian (normal, or bell-curve) distributed
102                 random value, with mean of 0.0 and standard deviation
103                 1.0. */

104
105     public double nextGaussian() {
106         double fac, rsq, v1, v2;
107
108         if (!iset) {
109             do {
110                 v1 = 2 * nextDouble() - 1;
111                 v2 = 2 * nextDouble() - 1;
112                 rsq = v1 * v1 + v2 * v2;
113             } while (rsq > 1.0 || rsq == 0.0);
114             fac = Math.sqrt(-2.0 * Math.log(rsq) / rsq);
115             gset = v1 * fac;
116             iset = true;
117             return v2 * fac;
118         } else {
119             iset = false;
120             return gset;
121         }
122     }
123
124     // Extended generator access methods with default implementations
125

126     private byte b;
127
128     /** @return the next random bit, as a <tt>boolean</tt>. */
129
130     public boolean nextBit() {
131         boolean bit;
132
133         if (nbits <= 0) {
134             b = nextByte();
135             nbits = 8;
136         }
137         bit = (b & 0x80) != 0;
138         b <<= 1;
139         nbits--;
140         return bit;
141     }
142
143     /** Fill a portion of an array of bytes with random data.
144
145 @param buf array of <tt>byte</tt> to fill.
146
147 @param buflen number of bytes to store.
148     */

149
150     public void nextByte(byte buf[], int buflen) {
151         int i = 0;
152
153         while (buflen-- > 0) {
154             buf[i++] = nextByte();
155         }
156     }
157
158     /** Fill an array of bytes with random data.
159
160 @param buf array of <tt>byte</tt>s to fill.
161     */

162
163     public void nextByte(byte buf[]) {
164         nextByte(buf, buf.length);
165     }
166
167     /** @return the next random, uniformly distributed, <tt>short</tt>
168                 value. */

169
170     public short nextShort() {
171         return (short) ((((short) nextByte()) << 8) | ((short) (nextByte() & 0xFF)));
172     }
173 }
174
Popular Tags