The class java.util.Random
can generate pseudo-random numbers having a reasonably uniform distribution. Given a List
of your service type:
List<String> services = new ArrayList<String>(
Arrays.asList("COMPUTER", "DATA", "PRINTER"));
it is easy to select one at random:
String s = services.get(rnd.nextInt(services.size()));
Similarly, one of a list of feedback values may be chosen:
List<String> feedbacks = new ArrayList<String>(
Arrays.asList("1", "0", "-1"));
String s = feedbacks.get(rnd.nextInt(feedbacks.size()));
One simple expedient to get a different distribution is to "stack the deck". For example,
Arrays.asList("1", "1", "1", "0", "0", "-1"));
would produce 1, 0, and -1 with probability 1/2, 1/3, and 1/6, respectively. You can arrange more elaborate partitions using nextGaussian()
and a suitable confidence interval.
This approach should only be used for generating test data.
Addendum: The Apache Commons Math Guide includes a chapter on Data Generation, with informative links and documentation concerning other probability distributions.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…