Random facts about ARC4RANDOM

During the first week of labs at Flatiron School, we ran into a method that intrigued me quite a bit. The method was designed to generate a random float between a lower and upper bound range. I’ve included it here:

#define ARC4RANDOM_MAX 0x100000000
-
(CGFloat)randomFloatBetweenNumber:(CGFloat)minRange andNumber:(CGFloat)maxRange
{
return ((float)arc4random() / ARC4RANDOM_MAX)
* (maxRange - minRange)
+ minRange;
}

I’ve seen random number generators before in excel during my college and Wall Street days. I immediately thought of the RAND function in excel.

= RANDBETWEEN ( Bottom , Top )

Although Objective-C is new to me, I could make out what most of the method’s syntax was doing. However, I wanted to take a deeper dive into the meaning behind “Arc4Random” and also discover some new ways to use it.

History

According to Apple documentation, Arc4 is another reference for RC4 (Rivest Cipher 4), one of the most used software-based cryptography ciphers in the world. The cipher is included in popular Internet protocols such as SSL (Secure Sockets Layer) and WEP (for wireless network security).

Ron Rivest originally designed RC4 in 1987 when he was working for RSA Security, Inc. Industry folks refer to the “RC” shortcode as “Ron’s Code.” When RC4 was first developed, it was a proprietary algorithm but was later leaked online in 1994. The name RC4 is trademarked, so RC4 was later referred to as ARCFOUR or ARC4 to avoid trademark problems.

How does it work?

Cryptography can be pretty complicated (Caesar Cipher anyone?), so I’ll try and keep this as simple as possible. After all, I’m still trying to learn myself.

Arc4Random uses 8-bit S-Boxes (substitution boxes) which function as lookup tables where you obscure the relationship between the key and the ciphertext. Basically, the S-Box takes in a number of a certain amount of bits, and returns an output number of a certain amount of bits. Here’s an example of an S-Box:

Source: Stack Overflow

Arc4Random S-Boxes have about 2^1700 different states, which means the total amount of substitution variation. This gives Arc4Random twice the substitution range of similar functions rand(3) and random(3), providing more randomness. In the end, the Arc4Random function only returns a pseudo-random number, which means it can never be perfectly random. Also, the function can provide a number between 0 and 4294967296 (0x100000000).

More Ways to Use Arc4Random

Below are a few additional ways you can use Arc4Random.

Find Random NSInteger Within Range

NSInteger randomNumber = arc4random() % 16;

Generating Random Lowercase NSString

- (NSString *)getRandomNSString {
return [NSString stringWithFormat:@"%c", arc4random_uniform(26) + 'a'];

Generating Random element from NSArray

NSUInteger randomIndex = arc4random() % [theArray count];

Randomize an NSMutableArray

for (int x = 0; x < [array count]; x++) {
int randInt = (arc4random() % ([array count] - x)) + x;
[array exchangeObjectAtIndex:x withObjectAtIndex:randInt];
}

Hope this provides more clarity! I’ve included Apple’s documentation below.

Apple Documentation: https://developer.apple.com/library/ios/documentation/System/Conceptual/ManPages_iPhoneOS/man3/arc4random.3.html