简体   繁体   中英

How do I determine whether a randomly generated number is a multiple of 150?

I'm creating a basic mystery box program using randomly generated numbers to determine what rarity item they get. However, if the number they get is a multiple of 150, I want them to get a particular item. I'm not sure how to determine whether the random number is a multiple of 150 though.

else if (num is in 150 times tables)
{
    code...
}

Use the Modulo Operator '%' and check the remainder.

if (num % 150 == 0)
{
    // It is divisable, do something
}
else
{
    // Not divisable, do something else
}

The modulo operator basically says "How many times can I divide the first number by the second, and what do I have left over?". In this example if num divided by 150 has 0 left over then it fits perfectly and you know it's a multiple.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM