objective-ccocos2d-iphonebooleanarc4random

random BOOLs in an efficient way for cocos2d


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 )


Solution

  • you can do something like this:

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