KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgap.util;
2
3
4 import org.jgap.*;
5
6 import java.util.Random JavaDoc;
7
8 /**
9     Implementation of a <b>randomX</b>-compliant class based upon the
10     built-in <tt>Java.util.Random</tt> generator. Note that since the higher
11     level result composition methods are different from those in the
12     (undocumented) standard library, <b>randomJava</b> results will differ
13     from those of the standard library for a given seed.
14
15     <p>
16     Designed and implemented in July 1996 by
17     <a HREF="http://www.fourmilab.ch/">John Walker</a>,
18     <a HREF="mailto:kelvin@fourmilab.ch">kelvin@fourmilab.ch</a>.
19 */

20 public class randomJava extends randomX {
21
22     private Random JavaDoc r;
23     private int ibytes = 0;
24
25     // Constructors
26

27     /** Creates a new pseudorandom number generator, seeded from
28         the current time. */

29
30     public randomJava() {
31         /* Since Java's generator seeds itself from the current time
32            when called with no arguments, we don't have to pass in a
33            seed here. */

34         r = new Random JavaDoc();
35     }
36
37     /** Creates a new pseudorandom number generator with a
38         specified seed.
39
40 @param seed initial seed for the generator
41     */

42
43     public randomJava(long seed) {
44         r = new Random JavaDoc(seed);
45     }
46
47     /** Set seed for generator. Subsequent values will be based
48         on the given seed.
49
50 @param seed seed for the generator
51     */

52
53     public void setSeed(long seed) {
54         super.setSeed(); // Notify parent seed has changed
55
r.setSeed(seed);
56         ibytes = 0; // Clear bytes in nextByte buffer
57
}
58
59     private int idat;
60
61     /** Get next byte from generator. To minimise calls on the
62         underlying Java generator, integers are generated and
63         individual bytes returned on subsequent calls. A call
64         on <tt>setSeed()</tt> discards any bytes in the buffer.
65
66 @return the next byte from the generator.
67     */

68
69     public byte nextByte() {
70         byte d;
71
72         if (ibytes <= 0) {
73             idat = r.nextInt();
74             ibytes = 4;
75         }
76         d = (byte) idat;
77         idat >>= 8;
78         ibytes--;
79         return d;
80     }
81 };
82
Popular Tags