How to create a random float in Objective-C?

前端 未结 8 1433
清酒与你
清酒与你 2020-12-24 06:06

I\'m trying to create a random float between 0.15 and 0.3 in Objective-C. The following code always returns 1:

int randn = (random() % 15)+15;
float pscale =         


        
8条回答
  •  醉梦人生
    2020-12-24 06:31

    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;
    }
    

提交回复
热议问题