summaryrefslogtreecommitdiff
path: root/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/RandomGenerator.java
blob: 5a3f4461259214ba2f4dbbc86d1d428d039fb55a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/**
 * Description: For generating random numbers.
 *
 * @ Author        Create/Modi     Note
 * Xiaofeng Xie    Feb 22, 2001
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * Please acknowledge the author(s) if you use this code in any way.
 *
 * @version 1.0
 * @Since MAOS1.0
 */

package net.adaptivebox.global;

public class RandomGenerator {

/**This function returns a random integer number between the lowLimit and upLimit.
 * @param lowLimit lower limits
 *        upLimit The upper limits (between which the random number is to be generated)
 * @return int return value
 * Example: for find [0,1,2]
*/
public static int intRangeRandom(int lowLimit,int upLimit){
  int num = (int)Math.floor(doubleRangeRandom(lowLimit,upLimit+1)-1E-10);
  return(num);
}

/**This function returns a random float number between the lowLimit and upLimit.
 * @param lowLimit lower limits
 *        upLimit The upper limits (between which the random number is to be generated)
 * @return double return value
*/
public static double doubleRangeRandom(double lowLimit,double upLimit){
  double num = lowLimit + Math.random()*(upLimit-lowLimit);
  return(num);
}

/**This function returns true or false with a random probability.
 * @return int return value
  */
  public static boolean booleanRandom(){
    boolean value = true;
    double temp=Math.random();
    if (temp<0.5) value=false;
    return value;
  }

  public static int[] randomSelection(boolean[] types, int times) {
    int validNum = 0;
    for(int i=0; i<types.length; i++) {
      if(!types[i]) {
        validNum++;
      }
    }
    int[] totalIndices = new int[validNum];
    validNum = 0;
    for(int i=0; i<types.length; i++) {
      if(!types[i]) {
        totalIndices[validNum] = i;
        validNum++;
        if(validNum==totalIndices.length) break;
      }
    }
    return randomSelection(totalIndices, times);
  }

  public static int[] randomSelection(int low, int up, int times){
    int[] totalIndices = new int[up-low];
    for (int i=low; i<up; i++) {
      totalIndices[i] = i;
    }
    return randomSelection(totalIndices, times);
  }

  public static int getRealV(double randTypeV) {
    if(randTypeV<=0) return 0;
    int realV = (int)Math.ceil(randTypeV);
    if(Math.random()<(randTypeV-realV)) realV++;
    return realV;
  }

  private static int[] randomSelection(int[] totalIndices, int times) {
    if (times>=totalIndices.length) {
      return totalIndices;
    }
    int[] indices = randomSelection(totalIndices.length, times);
    for(int i=0; i<indices.length; i++) {
      indices[i] = totalIndices[indices[i]];
    }
    return indices;
  }

  public static int[] randomSelection(int maxNum, int times) {
    if(times<=0) return new int[0];
    int realTimes = Math.min(maxNum, times);
    boolean[] flags = new boolean[maxNum];
//    Arrays.fill(flags, false);
    boolean isBelowHalf = times<maxNum*0.5;
    int virtualTimes = realTimes;
    if(!isBelowHalf) {
      virtualTimes = maxNum-realTimes;
    }
    int i = 0;
    int upper = maxNum-1;
    int[] indices = new int[realTimes];

    while(i<virtualTimes) {
      indices[i] = intRangeRandom(0, upper);
      if(!flags[indices[i]]) {
        flags[indices[i]] = true;
        i++;
      }
    }
    if(!isBelowHalf) {
      int j=0;
      for(i=0; i<maxNum; i++) {
        if(flags[i]==isBelowHalf) {
          indices[j] = i;
          j++;
          if(j==realTimes) break;
        }
      }
    }
    return indices;
  }
}