From 7542722436d02b65cceecd3166dec502ebf8cbd2 Mon Sep 17 00:00:00 2001 From: Jesper Eskilson Date: Wed, 24 Jun 2015 09:28:53 +0200 Subject: [PATCH] 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 --- .../dsf/dataviewer/DataGeneratorWithExecutor.java | 11 +++++------ .../dsf/dataviewer/DataGeneratorWithThread.java | 6 +++--- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/dsf/org.eclipse.cdt.examples.dsf/src_preprocess/org/eclipse/cdt/examples/dsf/dataviewer/DataGeneratorWithExecutor.java b/dsf/org.eclipse.cdt.examples.dsf/src_preprocess/org/eclipse/cdt/examples/dsf/dataviewer/DataGeneratorWithExecutor.java index 5022746f917..928deb352c8 100644 --- a/dsf/org.eclipse.cdt.examples.dsf/src_preprocess/org/eclipse/cdt/examples/dsf/dataviewer/DataGeneratorWithExecutor.java +++ b/dsf/org.eclipse.cdt.examples.dsf/src_preprocess/org/eclipse/cdt/examples/dsf/dataviewer/DataGeneratorWithExecutor.java @@ -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 changed = new HashMap(); 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); } diff --git a/dsf/org.eclipse.cdt.examples.dsf/src_preprocess/org/eclipse/cdt/examples/dsf/dataviewer/DataGeneratorWithThread.java b/dsf/org.eclipse.cdt.examples.dsf/src_preprocess/org/eclipse/cdt/examples/dsf/dataviewer/DataGeneratorWithThread.java index 3b4a909e884..0fbde0fa87e 100644 --- a/dsf/org.eclipse.cdt.examples.dsf/src_preprocess/org/eclipse/cdt/examples/dsf/dataviewer/DataGeneratorWithThread.java +++ b/dsf/org.eclipse.cdt.examples.dsf/src_preprocess/org/eclipse/cdt/examples/dsf/dataviewer/DataGeneratorWithThread.java @@ -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 changed = new HashMap(); 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); }