// onjava/RandomSupplier.java // Suppliers that produce random values. package onjava; import java.util.*; import java.util.function.*; public class RandomSupplier { private static Random r = new Random(47); public static class Boolean implements Supplier { @Override public java.lang.Boolean get() { return r.nextBoolean(); } } public static class Byte implements Supplier { @Override public java.lang.Byte get() { return (byte)r.nextInt(); } } public static class Character implements Supplier { @Override public java.lang.Character get() { return CountingSupplier.chars[ r.nextInt(CountingSupplier.chars.length)]; } } public static class String extends CountingSupplier.String { // Plug in the random Character generator: { cg = new Character(); } // Instance initializer public String() {} public String(int length) { super(length); } } public static class Short implements Supplier { @Override public java.lang.Short get() { return (short)r.nextInt(); } } public static class Integer implements Supplier { private int mod = 10000; public Integer() {} public Integer(int modulo) { mod = modulo; } @Override public java.lang.Integer get() { return r.nextInt(mod); } } public static class Long implements Supplier { private int mod = 10000; public Long() {} public Long(int modulo) { mod = modulo; } @Override public java.lang.Long get() { return (long) r.nextInt(mod); } } public static class Float implements Supplier { @Override public java.lang.Float get() { // Trim all but the first two decimal places: int trimmed = Math.round(r.nextFloat() * 100); return ((float)trimmed) / 100; } } public static class Double implements Supplier { @Override public java.lang.Double get() { long trimmed = Math.round(r.nextDouble() * 100); return ((double)trimmed) / 100; } } }