1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-06-06 17:26:01 +02:00

Cleaned up random number generation in DSF examples.

Fixed two FindBugs problems: (1) doing Math.abs(random.nextInt()) (which
may be negative if nextInt() returns Integer.MIN_VALUE), and (2)
creating a new Random() object for each nextInt() invocation.

Change-Id: I037a8f6c6c875c951a20beb315c54dc3759c963f
Signed-off-by: Jesper Eskilson <jesper.eskilson@iar.com>
This commit is contained in:
Jesper Eskilson 2015-06-24 09:28:53 +02:00 committed by Gerrit Code Review @ Eclipse.org
parent bb50c58c02
commit 7542722436
2 changed files with 8 additions and 9 deletions

View file

@ -180,7 +180,7 @@ public class DataGeneratorWithExecutor implements IDataGenerator {
public DataGeneratorWithExecutor(DsfExecutor executor) {
// Create the executor
fExecutor = executor;
final Random rand = new Random();
// Schedule a runnable to make the random changes.
fExecutor.scheduleAtFixedRate(
new DsfRunnable() {
@ -188,7 +188,7 @@ public class DataGeneratorWithExecutor implements IDataGenerator {
randomChanges();
}
},
new Random().nextInt() % RANDOM_CHANGE_INTERVAL,
rand.nextInt(RANDOM_CHANGE_INTERVAL),
RANDOM_CHANGE_INTERVAL, //Add a 10% variance to the interval.
TimeUnit.MILLISECONDS);
}
@ -409,8 +409,7 @@ public class DataGeneratorWithExecutor implements IDataGenerator {
private void randomCountReset() {
// Calculate the new count.
Random random = new java.util.Random();
fCount = MIN_COUNT +
Math.abs(random.nextInt()) % (MAX_COUNT - MIN_COUNT);
fCount = MIN_COUNT + random.nextInt(MAX_COUNT - MIN_COUNT);
// Reset the changed values.
fChangedValues.clear();
@ -435,8 +434,8 @@ public class DataGeneratorWithExecutor implements IDataGenerator {
Random random = new java.util.Random();
Map<Integer, Integer> changed = new HashMap<Integer, Integer>();
for (int i = 0; i < fCount * RANDOM_CHANGE_SET_PERCENTAGE / 100; i++) {
int randomIndex = Math.abs(random.nextInt()) % fCount;
int randomValue = Math.abs(random.nextInt()) % fCount;
int randomIndex = random.nextInt(fCount);
int randomValue = random.nextInt(fCount);
changed.put(randomIndex, randomValue);
}

View file

@ -222,7 +222,7 @@ public class DataGeneratorWithThread extends Thread
private void randomCountReset() {
// Calculate the new count.
Random random = new java.util.Random();
fCount = MIN_COUNT + Math.abs(random.nextInt()) % (MAX_COUNT - MIN_COUNT);
fCount = MIN_COUNT + random.nextInt(MAX_COUNT - MIN_COUNT);
// Reset the changed values.
fChangedValues.clear();
@ -238,8 +238,8 @@ public class DataGeneratorWithThread extends Thread
Random random = new java.util.Random();
Map<Integer, Integer> changed = new HashMap<Integer, Integer>();
for (int i = 0; i < fCount * RANDOM_CHANGE_SET_PERCENTAGE / 100; i++) {
int randomIndex = Math.abs(random.nextInt()) % fCount;
int randomValue = Math.abs(random.nextInt()) % fCount;
int randomIndex = random.nextInt(fCount);
int randomValue = random.nextInt(fCount);
changed.put(randomIndex, randomValue);
}