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, preferably without reading the data in from a file? I need a unique buffer of a specific size each time.
Thanks
The original version has a bug but mine takes care of that and hopefully doesn't introduce any new one. Hope it helps.
Here is its unit test:
I've open sourced my JFRandom class over at github which can do exactly this. Here's a blog post demonstrating how to obtain/use it to achieve your goal...
http://jayfuerstenberg.com/devblog/generating-random-numbers-strings-and-data-in-objective-c
You can create a 20*2^20b
NSData
object, then append a random 4 byte integer to it 20*2^20/4 times witharc4random()
. I believe you need to includestdlib.h
(via Generating random numbers in Objective-C).Here's a 3-liner swift version:
Swift 2
Swift 3
The bytes are not 'random', but will contain garbage values (whatever was on the heap before this was run). The advantage being its fast and the code is concise.
Use
arc4random_buf
to fill the buffer with random bytesObj-C