KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jgap.util;
2
3 import java.net.*;
4 import java.io.*;
5
6 /**
7     Implementation of a <b>randomX</b>-compliant class which obtains
8     genuine random data from <a HREF="http://www.fourmilab.ch/">John
9     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()
34         throws java.io.IOException JavaDoc
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     /** Get next byte from generator.
49
50 @return the next byte from the generator.
51     */

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 JavaDoc("Cannot obtain HotBits");
63         }
64     }
65 };
66
Popular Tags