|
| 1 | +package com.pholser.junit.quickcheck.random; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.time.Duration; |
| 5 | +import java.time.Instant; |
| 6 | +import java.util.Collection; |
| 7 | +import java.util.Random; |
| 8 | + |
| 9 | +public interface SourceOfRandomness { |
| 10 | + /** |
| 11 | + * <p>Gives a JDK source of randomness, with the same internal state as |
| 12 | + * this source of randomness.</p> |
| 13 | + * |
| 14 | + * @return a JDK "clone" of self |
| 15 | + */ |
| 16 | + Random toJDKRandom(); |
| 17 | + |
| 18 | + /** |
| 19 | + * @return a uniformly distributed boolean value |
| 20 | + * @see Random#nextBoolean() |
| 21 | + */ |
| 22 | + boolean nextBoolean(); |
| 23 | + |
| 24 | + /** |
| 25 | + * @param bytes a byte array to fill with random values |
| 26 | + * @see Random#nextBytes(byte[]) |
| 27 | + */ |
| 28 | + void nextBytes(byte[] bytes); |
| 29 | + |
| 30 | + /** |
| 31 | + * Gives an array of a given length containing random bytes. |
| 32 | + * |
| 33 | + * @param count the desired length of the random byte array |
| 34 | + * @return random bytes |
| 35 | + * @see Random#nextBytes(byte[]) |
| 36 | + */ |
| 37 | + byte[] nextBytes(int count); |
| 38 | + |
| 39 | + /** |
| 40 | + * @return a uniformly distributed random {@code double} value in the |
| 41 | + * interval {@code [0.0, 1.0)} |
| 42 | + * @see Random#nextDouble() |
| 43 | + */ |
| 44 | + double nextDouble(); |
| 45 | + |
| 46 | + /** |
| 47 | + * @return a uniformly distributed random {@code float} value in the |
| 48 | + * interval {@code [0.0, 1.0)} |
| 49 | + * @see Random#nextFloat() |
| 50 | + */ |
| 51 | + float nextFloat(); |
| 52 | + |
| 53 | + /** |
| 54 | + * @return a Gaussian-distributed random double value |
| 55 | + * @see Random#nextGaussian() |
| 56 | + */ |
| 57 | + double nextGaussian(); |
| 58 | + |
| 59 | + /** |
| 60 | + * @return a uniformly distributed random {@code int} value |
| 61 | + * @see Random#nextInt() |
| 62 | + */ |
| 63 | + int nextInt(); |
| 64 | + |
| 65 | + /** |
| 66 | + * @param n upper bound |
| 67 | + * @return a uniformly distributed random {@code int} value in the interval |
| 68 | + * {@code [0, n)} |
| 69 | + * @see Random#nextInt(int) |
| 70 | + */ |
| 71 | + int nextInt(int n); |
| 72 | + |
| 73 | + /** |
| 74 | + * @return a uniformly distributed random {@code long} value |
| 75 | + * @see Random#nextLong() |
| 76 | + */ |
| 77 | + long nextLong(); |
| 78 | + |
| 79 | + /** |
| 80 | + * @param seed value with which to seed this source of randomness |
| 81 | + * @see Random#setSeed(long) |
| 82 | + */ |
| 83 | + void setSeed(long seed); |
| 84 | + |
| 85 | + /** |
| 86 | + * @return the value used to initially seed this source of randomness |
| 87 | + */ |
| 88 | + long seed(); |
| 89 | + |
| 90 | + /** |
| 91 | + * Gives a random {@code byte} value, uniformly distributed across the |
| 92 | + * interval {@code [min, max]}. |
| 93 | + * |
| 94 | + * @param min lower bound of the desired interval |
| 95 | + * @param max upper bound of the desired interval |
| 96 | + * @return a random value |
| 97 | + */ |
| 98 | + byte nextByte(byte min, byte max); |
| 99 | + |
| 100 | + /** |
| 101 | + * Gives a random {@code char} value, uniformly distributed across the |
| 102 | + * interval {@code [min, max]}. |
| 103 | + * |
| 104 | + * @param min lower bound of the desired interval |
| 105 | + * @param max upper bound of the desired interval |
| 106 | + * @return a random value |
| 107 | + */ |
| 108 | + char nextChar(char min, char max); |
| 109 | + |
| 110 | + /** |
| 111 | + * <p>Gives a random {@code double} value in the interval |
| 112 | + * {@code [min, max)}.</p> |
| 113 | + * |
| 114 | + * <p>This naive implementation takes a random {@code double} value from |
| 115 | + * {@link Random#nextDouble()} and scales/shifts the value into the desired |
| 116 | + * interval. This may give surprising results for large ranges.</p> |
| 117 | + * |
| 118 | + * @param min lower bound of the desired interval |
| 119 | + * @param max upper bound of the desired interval |
| 120 | + * @return a random value |
| 121 | + */ |
| 122 | + double nextDouble(double min, double max); |
| 123 | + |
| 124 | + /** |
| 125 | + * <p>Gives a random {@code float} value in the interval |
| 126 | + * {@code [min, max)}.</p> |
| 127 | + * |
| 128 | + * <p>This naive implementation takes a random {@code float} value from |
| 129 | + * {@link Random#nextFloat()} and scales/shifts the value into the desired |
| 130 | + * interval. This may give surprising results for large ranges.</p> |
| 131 | + * |
| 132 | + * @param min lower bound of the desired interval |
| 133 | + * @param max upper bound of the desired interval |
| 134 | + * @return a random value |
| 135 | + */ |
| 136 | + float nextFloat(float min, float max); |
| 137 | + |
| 138 | + /** |
| 139 | + * Gives a random {@code int} value, uniformly distributed across the |
| 140 | + * interval {@code [min, max]}. |
| 141 | + * |
| 142 | + * @param min lower bound of the desired interval |
| 143 | + * @param max upper bound of the desired interval |
| 144 | + * @return a random value |
| 145 | + */ |
| 146 | + int nextInt(int min, int max); |
| 147 | + |
| 148 | + /** |
| 149 | + * Gives a random {@code long} value, uniformly distributed across the |
| 150 | + * interval {@code [min, max]}. |
| 151 | + * |
| 152 | + * @param min lower bound of the desired interval |
| 153 | + * @param max upper bound of the desired interval |
| 154 | + * @return a random value |
| 155 | + */ |
| 156 | + long nextLong(long min, long max); |
| 157 | + |
| 158 | + /** |
| 159 | + * Gives a random {@code short} value, uniformly distributed across the |
| 160 | + * interval {@code [min, max]}. |
| 161 | + * |
| 162 | + * @param min lower bound of the desired interval |
| 163 | + * @param max upper bound of the desired interval |
| 164 | + * @return a random value |
| 165 | + */ |
| 166 | + short nextShort(short min, short max); |
| 167 | + |
| 168 | + /** |
| 169 | + * Gives a random {@code BigInteger} representable by the given number |
| 170 | + * of bits. |
| 171 | + * |
| 172 | + * @param numberOfBits the desired number of bits |
| 173 | + * @return a random {@code BigInteger} |
| 174 | + * @see BigInteger#BigInteger(int, Random) |
| 175 | + */ |
| 176 | + BigInteger nextBigInteger(int numberOfBits); |
| 177 | + |
| 178 | + /** |
| 179 | + * Gives a random {@code Instant} value, uniformly distributed across the |
| 180 | + * interval {@code [min, max]}. |
| 181 | + * |
| 182 | + * @param min lower bound of the desired interval |
| 183 | + * @param max upper bound of the desired interval |
| 184 | + * @return a random value |
| 185 | + */ |
| 186 | + Instant nextInstant(Instant min, Instant max); |
| 187 | + |
| 188 | + /** |
| 189 | + * Gives a random {@code Duration} value, uniformly distributed across the |
| 190 | + * interval {@code [min, max]}. |
| 191 | + * |
| 192 | + * @param min lower bound of the desired interval |
| 193 | + * @param max upper bound of the desired interval |
| 194 | + * @return a random value |
| 195 | + */ |
| 196 | + Duration nextDuration(Duration min, Duration max); |
| 197 | + |
| 198 | + /** |
| 199 | + * Gives a random element of the given collection. |
| 200 | + * |
| 201 | + * @param <T> type of items in the collection |
| 202 | + * @param items a collection |
| 203 | + * @return a randomly chosen element from the collection |
| 204 | + */ |
| 205 | + <T> T choose(Collection<T> items); |
| 206 | + |
| 207 | + /** |
| 208 | + * Gives a random element of the given array. |
| 209 | + * |
| 210 | + * @param <T> type of items in the array |
| 211 | + * @param items an array |
| 212 | + * @return a randomly chosen element from the array |
| 213 | + */ |
| 214 | + <T> T choose(T[] items); |
| 215 | +} |
0 commit comments