[英]Converting a uint64_t rdtsc value to a uint32_t
I have a RNG function xorshift128plus that takes a Xorshift128PlusKey : 我有一个RNG功能xorshift128plus,需要一个Xorshift128PlusKey:
/**
* \brief Keys for scalar xorshift128. Must be non-zero.
* These are modified by xorshift128plus.
*/
struct Xorshift128PlusKey
{
uint64_t s1;
uint64_t s2;
};
/**
* \brief Return a new 64-bit random number.
*/
uint64_t xorshift128plus(Xorshift128PlusKey* key);
I would like to seed my RNG using rdtsc (processor time stamp). 我想使用rdtsc (处理器时间戳记)播种我的RNG。 The problem is that the
__rdtsc
intrinsic under msvc returns a 64 bit unsigned integer and the seed must be a 32 bit unsigned integer . 问题是msvc下的
__rdtsc
内在函数返回64位无符号整数 ,而种子必须是32位无符号整数 。 What is the best way to convert the rdtsc to a seed while preserving randomness . 在保留随机性的同时将rdtsc转换为种子的最佳方法是什么。 The conversion must be as fast as possible .
转换必须尽可能快 。
I can't use the std lib or boost . 我不能使用std lib或boost 。 (It's for a game engine)
(用于游戏引擎)
The 64-bit processor timestamp isn't random at all, so there is no need to preserve randomness when narrowing it to 32 bits. 64位处理器的时间戳完全不是随机的,因此将其范围缩小到32位时,无需保留随机性。 You can simply use the least-significant 32 bits as your seed.
您可以简单地使用最低有效的32位作为种子。 Randomness is the responsibility of the PRNG, not of the seed.
随机性是PRNG的责任,而不是种子的责任。
unsigned __int64 tsc = __rdtsc();
uint32_t seed = static_cast<uint32_t>(tsc & 0xFFFFFFFF);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.