1 package org.jgap.util; 2 3 import java.net.*; 4 import java.io.*; 5 6 17 public class randomHotBits extends randomX { 18 long state; 19 int nuflen = 256, buflen = 0; 20 byte[] buffer; 21 int bufptr = -1; 22 23 25 26 27 public randomHotBits() { 28 buffer = new byte[nuflen]; 29 } 30 31 32 33 private void fillBuffer() 34 throws java.io.IOException 35 { 36 URL u = new URL("http://www.fourmilab.ch/cgi-bin/uncgi/Hotbits?nbytes=128&fmt=bin"); 37 InputStream s = u.openStream(); 38 int l; 39 40 buflen = 0; 41 while ((l = s.read()) != -1) { 42 buffer[buflen++] = (byte) l; 43 } 44 s.close(); 45 bufptr = 0; 46 } 47 48 52 53 public byte nextByte() { 54 try { 55 synchronized (buffer) { 56 if (bufptr < 0 || bufptr >= buflen) { 57 fillBuffer(); 58 } 59 return buffer[bufptr++]; 60 } 61 } catch (IOException e) { 62 throw new RuntimeException ("Cannot obtain HotBits"); 63 } 64 } 65 }; 66 | Popular Tags |