Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Simple iPhone App with Objective-C Debugging Our App Breakpoints

Pablo Alfaro
PLUS
Pablo Alfaro
Courses Plus Student 6,923 Points

Confused about random number generated

I am little confused about the random number generated. The random number produces a large negative number and then it is converted into a smaller number within the bounds of the facts array. How is this number converted from large negative to a small positive number?

Here is the large negative number after pressing the 'show another fun fact' button my screenshot/ image link

Here is the generated number after stepping into the next steps my screenshot/ image link

1 Answer

I believe arc4random_uniform() returns essentially a 32-bit pseudo-random number between -2,147,483,648 (-1 × 231) through 2,147,483,647 (231 - 1), then adjusts it to positive. But with the limit you've put on it (the self.facts.count), it is partitioning that number range by your limit, and giving you the order of the partition (in this case the 2nd).