What's wrong with this randomize function?
cocoa-touch, iphone, objective-c, uikit
Solution
The manual page for `arc4random` indicates that the returned value can be anywhere in the range valid for `u int32` (i.e. `0 to (2**32)-1`). This means you'll want to divide by `0xFFFFFFFF`, instead of `RAND_MAX`, which I would imagine is less (it is library dependant however, so you'll have to check exactly what it is).
Your function should thus become:
- (float)randomValueBetween:(float)low andValue:(float)high {
return (((float) arc4random() / 0xFFFFFFFFu) * (high - low)) + low;
}
Problem
I give it 0 and 400 and it returns me sometimes values above 400. That doesn't make sense. ``` - (float)randomValueBetween:(float)low andValue:(float)high { return (((float) arc4random() / RAND_MAX) * (high - low)) + low; } ``` that's actually a snippet I found on the net. Maybe someone can see the bug in there?