Pages

Wednesday, January 30, 2013

Random Number in xCode

Here is the code to generate the random number between some 2 values.



- (int) randomNumberFrom: (int) minValue to: (int) maxValue
{
    double probability = rand() / 2147483648.0;
    double range = maxValue - minValue + 1;
    return (int)(range * probability + minValue);
}

- (int) randomNumberFrom: (int) minValue to: (int) maxValue except:(int) exceptValue
{
    if(minValue == maxValue)
    {
        return minValue;
    }
    int result;
    while(exceptValue == (result = [self randomNumberFrom:minValue to:maxValue]))
    {
    }
    return result;
}


Reference taken from:

http://www.cocos2d-iphone.org/forum/topic/28378

No comments:

Post a Comment