简体   繁体   中英

c++: Fastest way to fill a buffer with random bytes

I have this big char array that needs to filled with random bytes in high freq. I wonder if there is any faster way other than the naive way (using for loop - fill each cell with random byte) to do this. There is no requirement on the random quality of the values. Any "random" junk will do. Platform is windows

True random (Unix only):

int fd = open("/dev/random", O_RDONLY);
read(fd, your_buffer, buffer_size);

Not completely random (Unix only):

int fd = open("/dev/urandom", O_RDONLY);
read(fd, your_buffer, buffer_size);

Constant random (unless you use srand(time(NULL)) , portable):

for(size_t i = 0; i < buffer_size; i++)
    your_buffer[i] = rand() % 256;

Or something like:

memcpy(your_buffer, (void*)memcpy, buffer_size);

Depends if you're on Linux or Windows but on Linux doing a memcpy from /dev/random should work.

On Windows you can use CryptGenRandom to fill a buffer with random data: http://msdn.microsoft.com/en-us/library/aa379942.aspx . Apparently this is the Windows equivalent of reading data out of /dev/random. Python uses it to implement its OS.urandom function on Windows: http://en.wikipedia.org/wiki/CryptGenRandom

You could probably do something like, if your buffer size can be divided by 4.

unsigned int v = rand(), *ptr = (unsigned int *)buf;
for(int i = 0; i < buffer_size / 4; i++)
    ptr[i] = (v << 16) ^ rand();

Just an idea ;)

I have written a library that produces "almost random" buffers: using multiple buffers filled with pseudo-random random data, the library randomly pick a buffer and returns it to the application.

This library was designed first to be as fast as possible given memory consumption is cheap and bandwidth high.

It can be used for block based processing: the data produced is not really random, but the way the buffers are presented to the application is, so it generates a random stream which can be large enough to defeat some compression algorithm.

You can find it at: https://gitorious.org/randbuf/

A very fast and simple way of generating a large array of uniformly distributed random numbers is to use the Mersenne twister . If speed is critical, this could even be done using SIMD.

This seems to do what you need:

srandom(42);
memset(ptr, random(), len);

Only a single random number is generated but the data will be "random" enough to let you spot a lot of errors based on uninitialized memory. You can change the seed and rerun the program to test with different data.

If you need this for debugging you might also want to take a look at Valgrind .

Set up a buffer with junk values ahead. If you need to fill the char array with random bytes again, then just memcpy parts from the junk-buffer at random offsets to the char array until it is completely overwritten. memcpy is usually very fast and optimized to take advantage of SIMD and cache instructions. If you are copying segments large enough, then the overhead of selecting the random offsets if negligible - you are generating junk data with the speed of memcpy.

由于此问题标记为windows / winapi,因此您可以使用CryptGenRandom

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM