Skip to main content
summaryrefslogtreecommitdiffstats
path: root/dsf
diff options
context:
space:
mode:
authorJesper Eskilson2015-06-24 07:28:53 +0000
committerGerrit Code Review @ Eclipse.org2015-07-21 20:13:35 +0000
commit7542722436d02b65cceecd3166dec502ebf8cbd2 (patch)
tree5b781d56d60bc2cb2464989dedba93ad75eed251 /dsf
parentbb50c58c02c0687776555eb24c401946957c7053 (diff)
downloadorg.eclipse.cdt-7542722436d02b65cceecd3166dec502ebf8cbd2.tar.gz
org.eclipse.cdt-7542722436d02b65cceecd3166dec502ebf8cbd2.tar.xz
org.eclipse.cdt-7542722436d02b65cceecd3166dec502ebf8cbd2.zip
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>
Diffstat (limited to 'dsf')
-rw-r--r--dsf/org.eclipse.cdt.examples.dsf/src_preprocess/org/eclipse/cdt/examples/dsf/dataviewer/DataGeneratorWithExecutor.java11
-rw-r--r--dsf/org.eclipse.cdt.examples.dsf/src_preprocess/org/eclipse/cdt/examples/dsf/dataviewer/DataGeneratorWithThread.java6
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<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);
}
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<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);
}

Back to the top