How to create a random float in Objective-C? How to create a random float in Objective-C? objective-c objective-c

How to create a random float in Objective-C?


Here is a function

- (float)randomFloatBetween:(float)smallNumber and:(float)bigNumber {    float diff = bigNumber - smallNumber;    return (((float) (arc4random() % ((unsigned)RAND_MAX + 1)) / RAND_MAX) * diff) + smallNumber;}


Try this:

 (float)rand() / RAND_MAX

Or to get one between 0 and 5:

 float randomNum = ((float)rand() / RAND_MAX) * 5;

Several ways to do the same thing.


  1. use arc4random() or seed your random values
  2. try

    float pscale = ((float)randn) / 100.0f;