I had to generate random BOOL value to create a random error condition in my app and to test, how this error is handled.
So this is how I did it:
1 2 3 4 |
srand(time(0)); int randomval = rand() % 2; apiCall.shouldFailToLoad = (BOOL)randomval; NSLog(@"Should fail to load:%d", apiCall.shouldFailToLoad); |
We create a seed from current time moment for a rand function. Then we get a random number and a module from division by 2. It can be only 0 or 1. Then we tell our apiCall object to get a BOOL value, which tells it to fail sometimes.
To create a random integer value use this:
1 |
arc4random_uniform(upper_bound) |
It will return a uniformly distributed random number less than upper_bound. arc4random_uniform() is recommended over constructions like arc4random() % upper_bound” as it avoids “modulo bias” when the upper bound is not a power of two.
Reference: http://stackoverflow.com/questions/160890/generating-random-numbers-in-objective-c