random BOOLs in an efficient way for cocos2d

el.severo picture el.severo · Feb 6, 2012 · Viewed 7.9k times · Source

According to Steffen's post this is an efficient way to generate random BOOLs in cocos2d

+(BOOL) getYesOrNo
{
   return (CCRANDOM_0_1() < 0.5f);
}

but how do I set a range for this? (e.g. 0 - 29 is the interval and 5 ones BOOL = NO, 25 ones BOOL = YES )

Answer

nicoz_88 picture nicoz_88 · Feb 6, 2012

you can do something like this:

+(BOOL) getYesOrNo
{
    int tmp = (arc4random() % 30)+1;
    if(tmp % 5 == 0)
        return YES;
    return NO;
}