Generating random BOOL and int values in Objective C

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:

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:

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

Leave a Reply

Your email address will not be published. Required fields are marked *