Source code: com/arranger/jarl/util/RandomUtil.java
1 package com.arranger.jarl.util;
2
3 import java.util.ArrayList;
4 import java.util.Iterator;
5 import java.util.List;
6 import java.util.Random;
7
8 public class RandomUtil {
9
10 protected static java.util.Random m_random = new Random(System.currentTimeMillis());
11
12 public static void setSeed(long seed) {
13 m_random = new Random(seed);
14 }
15
16 public static int getRandom(int range) {
17 return (Math.abs(m_random.nextInt()) % range);
18 }
19
20 public static double getRandom(double range) {
21 return (Math.abs(m_random.nextDouble()) * range);
22 }
23
24 public static int getRandomRange(int rangeLow, int rangeHigh) {
25 return getRandom(rangeHigh - rangeLow) + rangeLow;
26 }
27
28 public static int getRandomRange(int rangeLow, int rangeHigh, int round) {
29 return (int)MathUtil.round(getRandomRange(rangeLow, rangeHigh), round);
30 }
31
32 public static double getRandomRange(double rangeLow, double rangeHigh) {
33 return getRandom(rangeHigh - rangeLow) + rangeLow;
34 }
35
36 public static double getRandomRange(double rangeLow, double rangeHigh, double round) {
37 return MathUtil.round(getRandomRange(rangeLow, rangeHigh), round);
38 }
39
40 /**
41 * odds must be between 0 & 1
42 */
43 public static boolean inRange(double odds) {
44 return (m_random.nextDouble() < odds);
45 }
46
47 public static boolean getEven() {
48 return (0 == getRandom(2));
49 }
50
51 public static Object getNextRandom(List set) {
52 return set.get(getRandom(set.size()));
53 }
54
55 public static Object getNextRandom(Object[] set) {
56 return set[getRandom(set.length)];
57 }
58
59 public static int getNextRandom(int[] set) {
60 return set[getRandom(set.length)];
61 }
62
63 public static int[] getNextRandom(int[][] set) {
64 return set[getRandom(set.length)];
65 }
66
67 public static double[] getNextRandom(double[][] set) {
68 return set[getRandom(set.length)];
69 }
70
71 public static double getNextRandom(double[] set) {
72 return set[getRandom(set.length)];
73 }
74
75 public static List randomizeSet(List set) {
76 List newSet = new ArrayList();
77 while (!set.isEmpty()) {
78
79 int index = getRandom(set.size());
80 newSet.add(set.get(index));
81 set.remove(index);
82 }
83
84 return newSet;
85 }
86
87 public static double[] randomizeSet(double[] set) {
88 List list = new ArrayList();
89 for (int index = 0; index < set.length; index++) {
90 list.add(new Integer(index));
91 }
92
93 list = randomizeSet(list);
94 double[] newSet = new double[set.length];
95 int index = 0;
96 for (Iterator it = list.iterator(); it.hasNext(); index++) {
97 Integer i = (Integer)it.next();
98 newSet[index] = set[i.intValue()];
99 }
100
101 return newSet;
102 }
103
104 public static int[] randomizeSet(int[] set) {
105 List list = new ArrayList();
106 for (int index = 0; index < set.length; index++) {
107 list.add(new Integer(index));
108 }
109
110 list = randomizeSet(list);
111 int[] newSet = new int[set.length];
112 int index = 0;
113 for (Iterator it = list.iterator(); it.hasNext(); index++) {
114 Integer i = (Integer)it.next();
115 newSet[index] = set[i.intValue()];
116 }
117 return newSet;
118 }
119 }