If I create a new NSData object of a specific size using dataWithBytes:length:, what is the most efficient way to create the input bytes (20 Mb worth) of random characters, pref
The original version has a bug but mine takes care of that and hopefully doesn't introduce any new one. Hope it helps.
- (NSData *)randomDataWithBytes: (NSUInteger)length {
NSMutableData *mutableData = [NSMutableData dataWithCapacity: length];
for (unsigned int i = 0; i < size; i++) {
NSInteger randomBits = arc4random();
[mutableData appendBytes: (void *) &randomBits length: 1];
} return mutableData;
}
Here is its unit test:
NSInteger givenLength = INT16_MAX;
NSData *randomData = [self randomDataWithBytes: givenLength];
STAssertTrue([randomData length] == givenLength,
@"RandomDataWithBytes Failed Expected size %d and got %d",
givenLength, [randomData length]);