org.jgap.util.randomHotBits Maven / Gradle / Ivy
package org.jgap.util;
import java.net.*;
import java.io.*;
/**
Implementation of a randomX-compliant class which obtains
genuine random data from John
Walker's HotBits
radioactive decay random sequence generator.
Designed and implemented in July 1996 by
John Walker,
[email protected].
*/
public class randomHotBits extends randomX {
long state;
int nuflen = 256, buflen = 0;
byte[] buffer;
int bufptr = -1;
// Constructors
/** Creates a new pseudorandom sequence generator. */
public randomHotBits() {
buffer = new byte[nuflen];
}
/* Private method to fill buffer from HotBits server. */
private void fillBuffer()
throws java.io.IOException
{
URL u = new URL("http://www.fourmilab.ch/cgi-bin/uncgi/Hotbits?nbytes=128&fmt=bin");
InputStream s = u.openStream();
int l;
buflen = 0;
while ((l = s.read()) != -1) {
buffer[buflen++] = (byte) l;
}
s.close();
bufptr = 0;
}
/** Get next byte from generator.
@return the next byte from the generator.
*/
public byte nextByte() {
try {
synchronized (buffer) {
if (bufptr < 0 || bufptr >= buflen) {
fillBuffer();
}
return buffer[bufptr++];
}
} catch (IOException e) {
throw new RuntimeException("Cannot obtain HotBits");
}
}
};