fdo#74697 Add Bluez 5 support for impress remote.
[LibreOffice.git] / nlpsolver / ThirdParty / EvolutionarySolver / src / net / adaptivebox / global / RandomGenerator.java
blob45204c44497d3bb85e751693b43bf2d90babe716
1 /**
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.
19 * @version 1.0
20 * @Since MAOS1.0
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);
36 return(num);
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);
46 return(num);
49 /**This function returns true or false with a random probability.
50 * @return int return value
52 public static boolean booleanRandom(){
53 boolean value = true;
54 double temp=Math.random();
55 if (temp<0.5) value=false;
56 return value;
59 public static int[] randomSelection(boolean[] types, int times) {
60 int validNum = 0;
61 for(int i=0; i<types.length; i++) {
62 if(!types[i]) {
63 validNum++;
66 int[] totalIndices = new int[validNum];
67 validNum = 0;
68 for(int i=0; i<types.length; i++) {
69 if(!types[i]) {
70 totalIndices[validNum] = i;
71 validNum++;
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++) {
86 // indices[i] = i;
87 // }
88 // return indices;
89 // }
90 // int i = 0;
91 // while(i<realTimes) {
92 // indices[i] = intRangeRandom(0, upper);
93 // if(!internalTypes[indices[i]]) {
94 // internalTypes[indices[i]] = true;
95 // i++;
96 // }
97 // }
98 // return indices;
99 // }
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++) {
104 totalIndices[i] = 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++;
113 return realV;
116 public static int[] randomSelection(int[] totalIndices, int times) {
117 if (times>=totalIndices.length) {
118 return totalIndices;
120 int[] indices = randomSelection(totalIndices.length, times);
121 for(int i=0; i<indices.length; i++) {
122 indices[i] = totalIndices[indices[i]];
124 return indices;
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;
134 if(!isBelowHalf) {
135 virtualTimes = maxNum-realTimes;
137 int i = 0;
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;
145 i++;
148 if(!isBelowHalf) {
149 int j=0;
150 for(i=0; i<maxNum; i++) {
151 if(flags[i]==isBelowHalf) {
152 indices[j] = i;
153 j++;
154 if(j==realTimes) break;
158 return indices;