Figure 2.19 Random Items Serial Number


I wouldn’t expect to get any random serial number that contains a number with the following code using %c format specifier, and indeed I’m not:

NSString *randomSerialNumber = [NSString stringWithFormat:@"%c%c%c%c%c",
                                'O' + arc4random() % 10,
                                'A' + arc4random() % 26,
                                'O' + arc4random() % 10,
                                'A' + arc4random() % 26,
                                'O' + arc4random() % 10];

But this differs from the serial numbers presented in Figure 2.19, which show serial numbers that DO contain numbers. The output from my code only shows serial numbers with upper case characters A-Z and no numbers. Should the randomSerialNumber from the above code contain numbers as well, given the %c format? Am I missing something here?


[color=#FF0000]O[/color]’ (ascii code for the letter ‘O’) should be ‘0’ (ascii code for the number zero.)


Thanks so much ibex10! Easily overlooked viewing my EPUB copy and should have checked my print copy of the book. Appreciate they quick reply!