The following example shows how to use Java to retrieve random data from an AlphaRNG device on Linux. The code only works while the run-alrng-pserver.sh script is running or the alrandom kernel module is loaded.
Retrieving random primitive values from an AlphaRNG device
package alpharng;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
/**
*
* Java example for retrieving random Java primitives from an AlphaRNG device when used with Linux.
*
*/
public class RetrieveRandomPrimitives {
public static void main(String[] args) throws IOException {
// Read from /tmp/alpharng when using run-alrng-pserver.sh script.
// Read from /dev/alrandom when using alrandom kernel module.
DataInputStream dis = new DataInputStream(new FileInputStream("/dev/alrandom"));
try {
System.out.println("Random Integer: " + dis.readInt());
System.out.println("Random Byte: " + dis.readByte());
System.out.println("Random Unsigned Byte: " + dis.readUnsignedByte());
System.out.println("Random Double: " + dis.readDouble());
System.out.println("Random Float: " + dis.readFloat());
System.out.println("Random Long: " + dis.readLong());
System.out.println("Random Short: " + dis.readShort());
System.out.println("Random Unsigned Short: " + dis.readUnsignedShort());
} catch (IOException e) {
// TODO Handle error
e.printStackTrace();
}
dis.close();
}
}