2 * Description: For generating random numbers.
4 * @ Author Create/Modi Note
5 * Xiaofeng Xie Feb 22, 2001
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * Please acknowledge the author(s) if you use this code in any way.
23 package net
.adaptivebox
.global
;
25 public class RandomGenerator
{
27 /**This function returns a random integer number between the lowLimit and upLimit.
28 * @param lowLimit lower limits
29 * upLimit The upper limits (between which the random number is to be generated)
30 * @return int return value
31 * Example: for find [0,1,2]
33 public static int intRangeRandom(int lowLimit
,int upLimit
){
34 // int num = (int)Math.rint(doubleRangeRandom(lowLimit,upLimit));
35 int num
= (int)Math
.floor(doubleRangeRandom(lowLimit
,upLimit
+1)-1E-10);
39 /**This function returns a random float number between the lowLimit and upLimit.
40 * @param lowLimit lower limits
41 * upLimit The upper limits (between which the random number is to be generated)
42 * @return double return value
44 public static double doubleRangeRandom(double lowLimit
,double upLimit
){
45 double num
= lowLimit
+ Math
.random()*(upLimit
-lowLimit
);
49 /**This function returns true or false with a random probability.
50 * @return int return value
52 public static boolean booleanRandom(){
54 double temp
=Math
.random();
55 if (temp
<0.5) value
=false;
59 public static int[] randomSelection(boolean[] types
, int times
) {
61 for(int i
=0; i
<types
.length
; i
++) {
66 int[] totalIndices
= new int[validNum
];
68 for(int i
=0; i
<types
.length
; i
++) {
70 totalIndices
[validNum
] = i
;
72 if(validNum
==totalIndices
.length
) break;
75 return randomSelection(totalIndices
, times
);
78 // public static int[] randomSelection(boolean[] types, int times) {
79 // int realTimes = times;
80 // if(realTimes>types.length) realTimes = types.length;
81 // boolean[] internalTypes = (boolean[])types.clone();
82 // int upper = types.length-1;
83 // int[] indices = new int[realTimes];
84 // if(realTimes==types.length) {
85 // for(int i=0; i<indices.length; i++) {
91 // while(i<realTimes) {
92 // indices[i] = intRangeRandom(0, upper);
93 // if(!internalTypes[indices[i]]) {
94 // internalTypes[indices[i]] = true;
101 public static int[] randomSelection(int low
, int up
, int times
){
102 int[] totalIndices
= new int[up
-low
];
103 for (int i
=low
; i
<up
; i
++) {
106 return randomSelection(totalIndices
, times
);
109 public static int getRealV(double randTypeV
) {
110 if(randTypeV
<=0) return 0;
111 int realV
= (int)Math
.ceil(randTypeV
);
112 if(Math
.random()<(randTypeV
-realV
)) realV
++;
116 public static int[] randomSelection(int[] totalIndices
, int times
) {
117 if (times
>=totalIndices
.length
) {
120 int[] indices
= randomSelection(totalIndices
.length
, times
);
121 for(int i
=0; i
<indices
.length
; i
++) {
122 indices
[i
] = totalIndices
[indices
[i
]];
127 public static int[] randomSelection(int maxNum
, int times
) {
128 if(times
<=0) return new int[0];
129 int realTimes
= Math
.min(maxNum
, times
);
130 boolean[] flags
= new boolean[maxNum
];
131 // Arrays.fill(flags, false);
132 boolean isBelowHalf
= times
<maxNum
*0.5;
133 int virtualTimes
= realTimes
;
135 virtualTimes
= maxNum
-realTimes
;
138 int upper
= maxNum
-1;
139 int[] indices
= new int[realTimes
];
141 while(i
<virtualTimes
) {
142 indices
[i
] = intRangeRandom(0, upper
);
143 if(!flags
[indices
[i
]]) {
144 flags
[indices
[i
]] = true;
150 for(i
=0; i
<maxNum
; i
++) {
151 if(flags
[i
]==isBelowHalf
) {
154 if(j
==realTimes
) break;