01: package org.jgap.util;
02:
03: import java.net.*;
04: import java.io.*;
05:
06: /**
07: Implementation of a <b>randomX</b>-compliant class which obtains
08: genuine random data from <a href="http://www.fourmilab.ch/">John
09: Walker</a>'s <a href="http://www.fourmilab.ch/hotbits/">HotBits</a>
10: radioactive decay random sequence generator.
11:
12: <p>
13: Designed and implemented in July 1996 by
14: <a href="http://www.fourmilab.ch/">John Walker</a>,
15: <a href="mailto:kelvin@fourmilab.ch">kelvin@fourmilab.ch</a>.
16: */
17: public class randomHotBits extends randomX {
18: long state;
19: int nuflen = 256, buflen = 0;
20: byte[] buffer;
21: int bufptr = -1;
22:
23: // Constructors
24:
25: /** Creates a new pseudorandom sequence generator. */
26:
27: public randomHotBits() {
28: buffer = new byte[nuflen];
29: }
30:
31: /* Private method to fill buffer from HotBits server. */
32:
33: private void fillBuffer() throws java.io.IOException {
34: URL u = new URL(
35: "http://www.fourmilab.ch/cgi-bin/uncgi/Hotbits?nbytes=128&fmt=bin");
36: InputStream s = u.openStream();
37: int l;
38:
39: buflen = 0;
40: while ((l = s.read()) != -1) {
41: buffer[buflen++] = (byte) l;
42: }
43: s.close();
44: bufptr = 0;
45: }
46:
47: /** Get next byte from generator.
48:
49: @return the next byte from the generator.
50: */
51:
52: public byte nextByte() {
53: try {
54: synchronized (buffer) {
55: if (bufptr < 0 || bufptr >= buflen) {
56: fillBuffer();
57: }
58: return buffer[bufptr++];
59: }
60: } catch (IOException e) {
61: throw new RuntimeException("Cannot obtain HotBits");
62: }
63: }
64: };
|