1 package edu.uci.ics.jung.algorithms.util;
2
3 import java.util.Random;
4
5 /**
6 * A decidedly non-random extension of {@code Random} that may be useful
7 * for testing random algorithms that accept an instance of {@code Random}
8 * as a parameter. This algorithm maintains internal counters which are
9 * incremented after each call, and returns values which are functions of
10 * those counter values. Thus the output is not only deterministic (as is
11 * necessarily true of all software with no externalities) but precisely
12 * predictable in distribution.
13 *
14 * @author Joshua O'Madadhain
15 */
16 @SuppressWarnings("serial")
17 public class NotRandom extends Random
18 {
19 private int i = 0;
20 private int d = 0;
21 private int size = 100;
22
23 /**
24 * Creates an instance with the specified sample size.
25 * @param size the sample size
26 */
27 public NotRandom(int size)
28 {
29 this.size = size;
30 }
31
32 /**
33 * Returns the post-incremented value of the internal counter modulo n.
34 */
35 @Override
36 public int nextInt(int n)
37 {
38 return i++ % n;
39 }
40
41 /**
42 * Returns the post-incremented value of the internal counter modulo
43 * {@code size}, divided by {@code size}.
44 */
45 @Override
46 public double nextDouble()
47 {
48 return (d++ % size) / (double)size;
49 }
50
51 /**
52 * Returns the post-incremented value of the internal counter modulo
53 * {@code size}, divided by {@code size}.
54 */
55 @Override
56 public float nextFloat()
57 {
58 return (d++ % size) / (float)size;
59 }
60 }