You can avoid pinching values ββwith mod (%) if you can, because even if the pseudo-random number generator that you use (e.g. arc4random ) is good for providing uniformly distributed numbers in the full range, it may not provide uniformly distributed numbers in within a limited range modulo.
You also don't need to use a literal like 0x100000000 , because in stdint.h:
there is a convenient constant
(float)arc4random() / UINT32_MAX
This will give you a random float in the interval [0,1]. Note that arc4random returns an integer in the interval [0, 2 ** 32 - 1].
To move this to the desired interval, you simply add your minimum value and multiply the random float by the size of your range:
lowerBound + ((float)arc4random() / UINT32_MAX) * (upperBound - lowerBound);
In the code you posted, you multiply a random float by the entire mess (lowerBound + (upperBound - lowerBound)), which is actually just equal to upperBound. And why are you still getting less results than your estimated lower bound.
Aaron Golden Jul 19. '14 at 2:50 2014-07-19 02:50
source share