将uint64_t rdtsc值转换为uint32_t

Cod*_*r32 1 c++ random integer rdtsc unsigned-integer

我有一个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);
Run Code Online (Sandbox Code Playgroud)

我想使用rdtsc(处理器时间戳)为我的RNG 播种.问题是__rdtscmsvc下的内在函数返回64位无符号整数,种子必须是32位无符号整数.什么是对RDTSC转换为种子的最佳方式,同时保留随机性.转换必须尽可能快.

我不能使用std libboost.(这是一个游戏引擎)

Typ*_*eIA 8

64位处理器时间戳根本不是随机的,因此在将其缩小到32位时无需保留随机性.您可以简单地使用最低有效32位作为种子.随机性是PRNG的责任,而不是种子的责任.

unsigned __int64 tsc = __rdtsc();
uint32_t seed = static_cast<uint32_t>(tsc & 0xFFFFFFFF);
Run Code Online (Sandbox Code Playgroud)