windows C++:用随机字节填充缓冲区的最快方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7427832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 18:04:58  来源:igfitidea点击:

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

c++windowswinapi

提问by GabiMe

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

我有这个需要用高频随机字节填充的大字符数组。我想知道除了天真的方法(使用 for 循环 - 用随机字节填充每个单元格)之外是否还有更快的方法来做到这一点。对值的随机质量没有要求。任何“随机”垃圾都可以。平台是windows

回答by wormsparty

True random (Unix only):

真随机(仅限 Unix):

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

Not completely random (Unix only):

不完全随机(仅限 Unix):

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

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

恒定随机(除非你使用srand(time(NULL)),便携):

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

Or something like:

或者类似的东西:

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

回答by sashang

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

取决于您使用的是 Linux 还是 Windows,但在 Linux 上从 /dev/random 执行 memcpy 应该可以工作。

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.urandomfunction on Windows: http://en.wikipedia.org/wiki/CryptGenRandom

在 Windows 上,您可以使用 CryptGenRandom 用随机数据填充缓冲区:http: //msdn.microsoft.com/en-us/library/aa379942.aspx。显然,这相当于从 /dev/random 读取数据的 Windows。Python 使用它OS.urandom在 Windows 上实现其功能:http: //en.wikipedia.org/wiki/CryptGenRandom

回答by brain

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

如果您的缓冲区大小可以除以 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 ;)

只是一个想法;)

回答by J?rgen Fogh

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.

如果您需要它进行调试,您可能还想看看Valgrind

回答by Nordic Mainframe

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.

设置一个带有垃圾值的缓冲区。如果您需要再次用随机字节填充 char 数组,则只需将垃圾缓冲区中的 memcpy 部分以随机偏移量分配到 char 数组,直到它被完全覆盖。memcpy 通常非常快并且经过优化以利用 SIMD 和缓存指令。如果您复制的段足够大,那么选择随机偏移的开销可以忽略不计 - 您正在以 memcpy 的速度生成垃圾数据。

回答by CommonSense

Since this question is labeled windows/winapi you could use CryptGenRandom.

由于这个问题被标记为 windows/winapi,你可以使用CryptGenRandom

回答by Yann Droneaud

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/

您可以在以下位置找到它:https: //gitorious.org/randbuf/

回答by Don Reba

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.

生成大量均匀分布的随机数的一种非常快速且简单的方法是使用梅森扭曲器。如果速度很重要,这甚至可以使用 SIMD 来完成。