diff options
author | Noel Grandin <noel@peralex.com> | 2014-11-11 15:32:10 +0200 |
---|---|---|
committer | Noel Grandin <noelgrandin@gmail.com> | 2014-11-12 08:01:55 +0000 |
commit | 36ff1527c9cb20542d3097d123d221c40a356795 (patch) | |
tree | 52590ce642803a862bc331b3eae2e2b100b8e85f /nlpsolver/ThirdParty/EvolutionarySolver | |
parent | 1eb31467a5af90fe41dc646dd716bdb7d3e5db45 (diff) |
java: reduce excessive code indentation levels
by using early return in some methods
Change-Id: I3611c8c89b3a94ef7e1772d178acf065fd7fcdc7
Reviewed-on: https://gerrit.libreoffice.org/12374
Reviewed-by: Noel Grandin <noelgrandin@gmail.com>
Tested-by: Noel Grandin <noelgrandin@gmail.com>
Diffstat (limited to 'nlpsolver/ThirdParty/EvolutionarySolver')
-rw-r--r-- | nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/RandomGenerator.java | 114 |
1 files changed, 58 insertions, 56 deletions
diff --git a/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/RandomGenerator.java b/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/RandomGenerator.java index 448ae01d9d64..86103ee97b3f 100644 --- a/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/RandomGenerator.java +++ b/nlpsolver/ThirdParty/EvolutionarySolver/src/net/adaptivebox/global/RandomGenerator.java @@ -24,65 +24,67 @@ 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); -} - - - - - - - - - - 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]; - boolean isBelowHalf = times<maxNum*0.5; - int virtualTimes = realTimes; - if(!isBelowHalf) { - virtualTimes = maxNum-realTimes; + /** + * 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); } - 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++; - } + /** + * 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); } - if(!isBelowHalf) { - int j=0; - for(i=0; i<maxNum; i++) { - if(flags[i]==isBelowHalf) { - indices[j] = i; - j++; - if(j==realTimes) break; + + 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]; + 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; } - return indices; - } }
\ No newline at end of file |