c++ - VS2010中random_device的实现?

标签 c++ visual-studio-2010 random c++11

根据我对标准的阅读,如果使用软件引擎,random_device::entropy() 应该返回 0.0。但是,在 VS2010 中,它返回 32.0,据我了解,这需要硬件来产生不确定的随机数。

VS2010的random_device如何生成数列?

最佳答案

根据 Hans Passant 对此问题的评论,random_device使用 advapi32:SystemFunction036 ,根据 MSDN,它是 RtlGenRandom 的别名. VC++ 2010 提供的运行时库源验证了这一点:

random_device::operator()()<random>调用以下函数链:

_Random_device() // in xrngdev.cpp
    rand_s()     // in rand_s.c
        RtlGenRandom()/SystemFunction036()  // in advapi32.dll
        

根据 Michael Howard 在他的一篇博客文章中留下的评论,"Cryptographically Secure Random number on Windows without using CryptoAPI" , RtlGenRandom使用以下内容:

The RNG generates as specified in FIPS 186-2 appendix 3.1 with SHA-1 as the G function. With entropy from:

  • The current process ID (GetCurrentProcessID).

  • The current thread ID (GetCurrentThreadID).

  • The ticks since boot (GetTickCount).

  • The current time (GetLocalTime).

  • Various high-precision performance counters (QueryPerformanceCounter).

  • An MD4 hash of the user's environment block, which includes username, computer name, and search path. MD4 is a hashing algorithm that creates a 128-bit message digest from input data to verify data integrity.

  • High-precision internal CPU counters, such as RDTSC, RDMSR, RDPMC

  • Low-level system information: Idle Process Time, Io Read Transfer Count, I/O Write Transfer Count, I/O Other Transfer Count, I/O Read Operation Count, I/O Write Operation Count, I/O Other Operation Count, Available Pages, Committed Pages, Commit Limit, Peak Commitment, Page Fault Count, Copy On Write Count, Transition Count, Cache Transition Count, Demand Zero Count, Page Read Count, Page Read I/O Count, Cache Read Count, Cache I/O Count, Dirty Pages Write Count, Dirty Write I/O Count, Mapped Pages Write Count, Mapped Write I/O Count, Paged Pool Pages, Non Paged Pool Pages, Paged Pool Allocated space, Paged Pool Free space, Non Paged Pool Allocated space, Non Paged Pool Free space, Free System page table entry, Resident System Code Page, Total System Driver Pages, Total System Code Pages, Non Paged Pool Lookaside Hits, Paged Pool Lookaside Hits, Available Paged Pool Pages, Resident System Cache Page, Resident Paged Pool Page, Resident System Driver Page, Cache manager Fast Read with No Wait, Cache manager Fast Read with Wait, Cache manager Fast Read Resource Missed, Cache manager Fast Read Not Possible, Cache manager Fast Memory Descriptor List Read with No Wait, Cache manager Fast Memory Descriptor List Read with Wait, Cache manager Fast Memory Descriptor List Read Resource Missed, Cache manager Fast Memory Descriptor List Read Not Possible, Cache manager Map Data with No Wait, Cache manager Map Data with Wait, Cache manager Map Data with No Wait Miss, Cache manager Map Data Wait Miss, Cache manager Pin-Mapped Data Count, Cache manager Pin-Read with No Wait, Cache manager Pin Read with Wait, Cache manager Pin-Read with No Wait Miss, Cache manager Pin-Read Wait Miss, Cache manager Copy-Read with No Wait, Cache manager Copy-Read with Wait, Cache manager Copy-Read with No Wait Miss, Cache manager Copy-Read with Wait Miss, Cache manager Memory Descriptor List Read with No Wait, Cache manager Memory Descriptor List Read with Wait, Cache manager Memory Descriptor List Read with No Wait Miss, Cache manager Memory Descriptor List Read with Wait Miss, Cache manager Read Ahead IOs, Cache manager Lazy-Write IOs, Cache manager Lazy-Write Pages, Cache manager Data Flushes, Cache manager Data Pages, Context Switches, First Level Translation buffer Fills, Second Level Translation buffer Fills, and System Calls.

  • System exception information consisting of Alignment Fix up Count, Exception Dispatch Count, Floating Emulation Count, and Byte Word Emulation Count.

  • System lookaside information consisting of Current Depth, Maximum Depth, Total Allocates, Allocate Misses, Total Frees, Free Misses, Type, Tag, and Size.

  • System interrupt information consisting of context switches, deferred procedure call count, deferred procedure call rate, time increment, deferred procedure call bypass count, and asynchronous procedure call bypass count.

  • System process information consisting of Next Entry Offset, Number Of Threads, Create Time, User Time, Kernel Time, Image Name, Base Priority, Unique Process ID, Inherited from Unique Process ID, Handle Count, Session ID, Page Directory Base, Peak Virtual Size, Virtual Size, Page Fault Count, Peak Working Set Size, Working Set Size, Quota Peak Paged Pool Usage, Quota Paged Pool Usage, Quota Peak Non Paged Pool Usage, Quota Non Paged Pool Usage, Page file Usage, Peak Page file Usage, Private Page Count, Read Operation Count, Write Operation Count, Other Operation Count, Read Transfer Count, Write Transfer Count, and Other Transfer Count.

在编写安全代码,第 2 版的第 8 章中有完整的解释(包括图表)。

关于c++ - VS2010中random_device的实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9549357/

相关文章:

c++ - 通过内部函数或指令手动控制 Intel MIC SIMD 操作

c++ - 使用来自另一个包的少量 C++ 级代码

c++ - Boost.Regex 与 C++11 正则表达式

visual-studio-2010 - visual studio 2010打不开项目类型()不受此版本支持

visual-studio-2010 - 如何将 Windows 组作为 “Readers” 添加到 TFS 2010 集合中的所有项目?

java - 从 5 个骰子掷骰中,生成一个范围 [1 - 100] 内的随机数

c++ - std::max 在统一初始化和 constexpr int 下表现出乎意料

c# - Visual Studio 文本编辑器扩展

使用 Mersenne Twister 生成 C++ 随机数

C 中的循环 #define