Permalink
Browse files

Simplify random upgrade drop calculations

There doesn't seem to be a particular advantage to using Random here. If
Random must be used, a universal Random object would suffice.
  • Loading branch information...
1 parent 2563fbc commit 4e07a3cd020e874dab3cc1370aa2310082d42111 @guoguo12 guoguo12 committed Apr 8, 2013
Showing with 5 additions and 15 deletions.
  1. +5 −15 GDCalaga/src/org/gdc/gdcalaga/Enemy.java
@@ -243,23 +243,13 @@ public void Hurt(float dmg)
*/
private void tryDropUpgrade()
{
- Random rand = new Random(System.currentTimeMillis());
- //percentageChance 1 - 100
- final int percentageChance = 5;
- int max = (100 / percentageChance);
- int min = 1;
- int chance = rand.nextInt(max - min + 1) + min;
- if (chance == (int)(max / 2)) //chance can be compared to any number between min and max
+ final double decimalChance = 0.05; // TODO Make static final
+ if (Math.random() < decimalChance)
{
//generate a random upgrade
- int numUpgrades = UpgradeType.getNumDroppableUpgrades();
- int minUpgrade = 1;
- chance = rand.nextInt(numUpgrades - minUpgrade + 1) + minUpgrade;
-
- Upgrade.UpgradeType type;
- type = UpgradeType.getIndexedUpgrade(chance);
-
- Upgrade upgrade = new Upgrade(entities, this.pos, type);
+ int numUpgrades = UpgradeType.getNumDroppableUpgrades();
+ Upgrade.UpgradeType type = UpgradeType.getIndexedUpgrade((int) (numUpgrades * Math.random()) + 1); // Add 1 because the indices start at 1
+ new Upgrade(entities, this.pos, type);
}
}
}

0 comments on commit 4e07a3c

Please sign in to comment.