Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random range of float in Objective C?

for an in I'm doing this:

int randomRange(int min, int max) {
    int range = max - min;
    if (range == 0) return min;

    return (arc4random() % range) + min;
 }

but how can I do this with a float?

like image 490
cocos2dbeginner Avatar asked Dec 21 '22 15:12

cocos2dbeginner


1 Answers

+(float) randFloatBetween:(float)low and:(float)high
{
    float diff = high - low;
    return (((float) rand() / RAND_MAX) * diff) + low;
}
like image 71
DenverCoder9 Avatar answered Jan 11 '23 00:01

DenverCoder9