Example using arc4random_uniform to generate random number

It’s been a busy day as it relates to random numbers, including: generating random numbers and avoiding modulo bias

Ramsey Dow wrote and shared an everyday example using arc4random_uniform:

Instead of jumping through hoops to avoid modulo bias, make use of arc4random_uniform. This function is just like arc4random, except that it takes a u_int32_t that defines the upper bound. For example, to simulate rolling a 6-sided die without modulo bias:

uint32_t score = arc4random_uniform(6)+1;

It’s that simple. The +1 at the end is to coerce the result into origin 1 (by default, score will range from 0..5, and we’d prefer 1..6).