Reputation:
I saw the libgdx MathUtils.randomBoolean(chances)
, I guess this would help me but I'm not sure.
MathUtils.randomBoolean(10); // I'm not sure if this will give 10% chance?
Upvotes: 0
Views: 1214
Reputation: 57964
Take a look at the LibGDX Javadocs regarding MathUtils
:
randomBoolean
public static boolean randomBoolean(float chance)
Returns true if a random value between 0 and 1 is less than the specified value.
That means if you specify a number (passed as an argument), the method will return true if the randomly generated number (between 0 and 1) is less than the passed chance
. In this case it would be:
MathUtils.randomBoolean(0.1);
This is because 0.1 is 10%, or 10/100. Thus, a random number between 0 and 1, if less than 0.1, will cause the method to return true.
Your code previously would always return true because a number between 0 and 1 is always less than 10.
Upvotes: 3
Reputation: 18855
MathUtils.randomBoolean(float chance)
gives the true
with probability given by the parameter. But the parameter chance
can take value between 0 - 1
, meaning that for example 0.1
gives 10%
(0.1
) probability of returning true
.
Your example - 10
- would always result in true
as it's bigger than 1
.
Upvotes: 5