/****************************************************************************** * This program implements the ACORN pseudo-random number generator. * * Copyright © 2021 Richard Lesh. All rights reserved. *****************************************************************************/ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.pureprogrammer.Utils; public class ACORN_PRNG_Test { static class ACORN_PRNG { private long modulus; private int order; private List seeds = new ArrayList<>(); public ACORN_PRNG(int order) { this.modulus = 4294967295L; this.order = order; for (int m = 0; m <= order; ++m) { long s = (long)(modulus * Math.random()); if (s % 2 == 0) { ++s; } seeds.add(s); } } public long nextInt() { for (int m = 1; m <= order; ++m) { seeds.set(m, (m == 1 ? seeds.get(0) : seeds.get(m - 1)) + seeds.get(m)); seeds.set(m, seeds.get(m) & modulus); } return seeds.get(order); } public double nextFloat() { return nextInt() / (modulus + 1.0); } } public static void main(String[] args) { if (args.length == 1 && args[0].equals("-h") ) { System.out.println("Syntax: " + "ACORN_PRNG_Test" + " [num_random] [int|float]"); System.out.println("If no arguments are provided a random stream of bytes"); System.out.println("are output."); System.exit(1); } int num = args.length >= 1 ? Utils.stoiWithDefault(args[0], 10) : -1; String type = args.length == 2 ? args[1] : "int"; System.out.println("type: " + (type.equals("int") ? "d" : "f")); System.out.println("count: " + num); System.out.println("numbit: 32"); ACORN_PRNG generator = new ACORN_PRNG(10); if (type.equals("int") ) { if (num < 1) { while (true) { int randint = (int)(generator.nextInt()); byte b = (byte)(randint & 0xFF); System.out.write(b); randint >>= 8; b = (byte)(randint & 0xFF); System.out.write(b); randint >>= 8; b = (byte)(randint & 0xFF); System.out.write(b); randint >>= 8; b = (byte)(randint & 0xFF); System.out.write(b); } } else { for (int i = 0; i < num; ++i) { System.out.println(generator.nextInt()); } } } else { for (int i = 0; i < num; ++i) { System.out.println(Utils.format("{0:.10f}", generator.nextFloat())); } } } }