Move to SecureRandom for better Random number generation.

This commit is contained in:
Fabian Becker 2014-10-07 16:08:12 +02:00
parent 5b1f696fa4
commit 3e3df4d8a2
3 changed files with 18 additions and 20 deletions

View File

@ -1,4 +1,4 @@
package eva2.optimization.problems; package eva2.problems;
import eva2.optimization.individuals.AbstractEAIndividual; import eva2.optimization.individuals.AbstractEAIndividual;
import eva2.optimization.strategies.InterfaceOptimizer; import eva2.optimization.strategies.InterfaceOptimizer;
@ -13,7 +13,7 @@ import java.io.Writer;
* with the given frequency. * with the given frequency.
*/ */
@Description("A real valued problem jumping dynamically.") @Description("A real valued problem jumping dynamically.")
public class DynJumpProblem extends AbstractDynTransProblem { public class DynJumpProblem extends eva2.problems.AbstractDynTransProblem {
private static final long serialVersionUID = 2693154860448970283L; private static final long serialVersionUID = 2693154860448970283L;

View File

@ -5,7 +5,7 @@ import eva2.tools.math.RNG;
import eva2.util.annotation.Description; import eva2.util.annotation.Description;
import java.util.Arrays; import java.util.Arrays;
import java.util.Random; import java.security.SecureRandom;
/** /**
* Fletcher-Powell function with up to 2^n optima from Shir&Baeck, PPSN 2006, * Fletcher-Powell function with up to 2^n optima from Shir&Baeck, PPSN 2006,
@ -39,14 +39,13 @@ public class F19Problem extends AbstractProblemDouble implements
@Override @Override
public void initializeProblem() { public void initializeProblem() {
super.initializeProblem(); super.initializeProblem();
// if (alphas==null) {
// create static random data // create static random data
Random rand = new Random(randSeed); SecureRandom rand = new SecureRandom();
rand.setSeed(randSeed);
alphas = RNG.randomDoubleArray(rand, -Math.PI, Math.PI, dim); alphas = RNG.randomDoubleArray(rand, -Math.PI, Math.PI, dim);
A = RNG.randomIntArray(rand, -100, 100, dim * dim); A = RNG.randomIntArray(rand, -100, 100, dim * dim);
B = RNG.randomIntArray(rand, -100, 100, dim * dim); B = RNG.randomIntArray(rand, -100, 100, dim * dim);
As = transform(alphas); As = transform(alphas);
// }
} }
private double[] transform(double[] x) { private double[] transform(double[] x) {

View File

@ -4,7 +4,7 @@ import eva2.tools.EVAERROR;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.BitSet; import java.util.BitSet;
import java.util.Random; import java.security.SecureRandom;
/** /**
* Random number generator used across all optimizations * Random number generator used across all optimizations
@ -12,7 +12,7 @@ import java.util.Random;
*/ */
public class RNG { public class RNG {
private static Random random; private static SecureRandom random;
private static long randomSeed; private static long randomSeed;
/** /**
@ -20,7 +20,8 @@ public class RNG {
*/ */
static { static {
randomSeed = System.currentTimeMillis(); randomSeed = System.currentTimeMillis();
random = new Random(randomSeed); random = new SecureRandom();
random.setSeed(randomSeed);
} }
/** /**
@ -32,7 +33,8 @@ public class RNG {
if (randomSeed == 0) { if (randomSeed == 0) {
setRandomSeed(); setRandomSeed();
} else { } else {
random = new Random(randomSeed); random = new SecureRandom();
random.setSeed(randomSeed);
} }
} }
@ -49,13 +51,14 @@ public class RNG {
*/ */
public static void setRandomSeed() { public static void setRandomSeed() {
randomSeed = System.currentTimeMillis(); randomSeed = System.currentTimeMillis();
random = new Random(randomSeed); random = new SecureRandom();
random.setSeed(randomSeed);
} }
/** /**
* *
*/ */
public static void setRandom(Random baseRandom) { public static void setRandom(SecureRandom baseRandom) {
random = baseRandom; random = baseRandom;
} }
@ -138,7 +141,7 @@ public class RNG {
* @param hi Upper bound. * @param hi Upper bound.
* @return int * @return int
*/ */
public static int randomInt(Random rand, int lo, int hi) { public static int randomInt(SecureRandom rand, int lo, int hi) {
if (hi < lo) { if (hi < lo) {
System.err.println("Invalid boundary values! Returning zero."); System.err.println("Invalid boundary values! Returning zero.");
return -1; return -1;
@ -193,7 +196,7 @@ public class RNG {
return (hi - lo) * random.nextDouble() + lo; return (hi - lo) * random.nextDouble() + lo;
} }
public static double randomDouble(Random rand, double lo, double hi) { public static double randomDouble(SecureRandom rand, double lo, double hi) {
return (hi - lo) * rand.nextDouble() + lo; return (hi - lo) * rand.nextDouble() + lo;
} }
@ -234,13 +237,9 @@ public class RNG {
result[i] = RNG.randomDouble(lower, upper); result[i] = RNG.randomDouble(lower, upper);
} }
return result; return result;
// double[] xin = new double[size];
// for (int i=0;i<size;i++)
// xin[i] = (hi-lo)*random.nextDouble()+lo;
// return xin;
} }
public static double[] randomDoubleArray(Random rand, double lower, double upper, int size) { public static double[] randomDoubleArray(SecureRandom rand, double lower, double upper, int size) {
double[] result = new double[size]; double[] result = new double[size];
for (int i = 0; i < result.length; i++) { for (int i = 0; i < result.length; i++) {
result[i] = RNG.randomDouble(rand, lower, upper); result[i] = RNG.randomDouble(rand, lower, upper);
@ -276,7 +275,7 @@ public class RNG {
return result; return result;
} }
public static int[] randomIntArray(Random rand, int lower, int upper, int size) { public static int[] randomIntArray(SecureRandom rand, int lower, int upper, int size) {
int[] result = new int[size]; int[] result = new int[size];
for (int i = 0; i < result.length; i++) { for (int i = 0; i < result.length; i++) {
result[i] = RNG.randomInt(rand, lower, upper); result[i] = RNG.randomInt(rand, lower, upper);