From cdf8c96c94ec2f4826d4b71842f5c5b4e14f0afe Mon Sep 17 00:00:00 2001 From: Treeku Date: Mon, 16 Jun 2014 10:40:45 +0100 Subject: [PATCH] Added tryChance method to MathUtilities --- src/resources/common/MathUtilities.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/resources/common/MathUtilities.java b/src/resources/common/MathUtilities.java index 8decf30b..b6159ac0 100644 --- a/src/resources/common/MathUtilities.java +++ b/src/resources/common/MathUtilities.java @@ -22,6 +22,7 @@ package resources.common; import java.math.BigDecimal; +import java.util.Random; import engine.resources.scene.*; @@ -68,4 +69,25 @@ public class MathUtilities { BigDecimal dec = new BigDecimal(seconds); return dec.remainder(new BigDecimal(3600)).divide(new BigDecimal(60), BigDecimal.ROUND_FLOOR).intValue(); } + + public static boolean tryChance(int chance) { + if (chance <= 0) { + return false; + } + + if (chance >= 100) { + return true; + } + + chance = (100 - chance); + + Random random = new Random(); + + if (random.nextInt(chance + 1) == 1) { + return true; + } + + return false; + } + }