// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
import java.util.Random;

public class RNGTest {
    public static interface RandomFoundation {
        public int getRandom();
    }
    @FunctionalInterface
    public static interface Candidate {
        public int getRandom(RandomFoundation rf);
    }
    public static class RandomFoundationN implements RandomFoundation {
        private int n;
        private Random rng;
        public RandomFoundationN(int n) {
            this.n = n;
            this.rng = new Random();
        }
        @Override
        public int getRandom() {
            return rng.nextInt(n);
        }
    }
    public static class RandomFoundation10 extends RandomFoundationN {
        public RandomFoundation10() {
            super(10);
        }
    }
    private static final int REP = 100000;
    private static final double ALLOWED_VAR = 0.01;
    public static void test(String lbl, Candidate c, RandomFoundation rf, int minincl, int maxincl) {
        int noval = maxincl - minincl + 1;
        int[] count = new int[noval];
        for(int i = 0; i < noval * REP; i++) {
            int val = c.getRandom(rf);
            if(val < minincl || maxincl < val) {
                System.out.printf("%s : Hopeless algorithm - value %d out of range\n", lbl, val);
                return;
            }
            count[val - minincl]++;
        }
        for(int i = 0; i < noval; i++) {
            if(Math.abs(count[i] - REP) > REP * ALLOWED_VAR){
                System.out.printf("%s : Bad algorithm - count %d of value %d not uniform\n", lbl, count[i], i + minincl);
                return;
            }
        }
        System.out.printf("%s : Good algorithm\n", lbl);
    }
    public static void main(String[] args) {
        test("Arne", (rf) -> 101 + 10 * rf.getRandom() + rf.getRandom(), new RandomFoundation10(), 101, 200);
    }
}
