This site requires JavaScript, please enable it in your browser!
Greenfoot back
Ruv25808
Ruv25808 wrote ...

2017/5/16

Help interpreting some code

Ruv25808 Ruv25808

2017/5/16

#
Hello. I'm having trouble interpreting what this code does could anyone be of assistance? protected boolean randomChance(int percent) { return Greenfoot.getRandomNumber(100) < percent; } Thanks in advance.
Super_Hippo Super_Hippo

2017/5/16

#
Greenfoot.getRandomNumber(100) returns a random number between 0 and 99. When you use the randomChance method, there is a 'percent' chance of returning 'true'. For example you use:
if (randomChance(60))
{
    //do something
}
Then percent is 60. 60 possible outcomes of the 100 possible outcomes of Greenfoot.getRandomNumber(100) are below 60 (= 0, 1, ..., 58, 59), so there is a 60 % chance that the condition is met.
Ruv25808 Ruv25808

2017/5/16

#
Oh I see. Thanks.
You need to login to post a reply.