More CPU/time-efficient way to get N quasi random

2019-08-31 01:20发布

问题:

This is a continuation of another question about efficiently getting quasi-random numbers.

I need to get N unique quasi-random numbers, bias/distribution quality is no big deal. Can I get them more CPU/time-efficiently than using N calls to rand() or /dev/random etc? Maybe using some math/bitwise manipulations on few random numbers or something like that. Or using precomputed tables or...

N can be quite big like 10000 or 1000000.

Thanks.

回答1:

I found simething like this here:

m_w = <choose-initializer>;    /* must not be zero */
m_z = <choose-initializer>;    /* must not be zero */

uint get_random()
{
    m_z = 36969 * (m_z & 65535) + (m_z >> 16);
    m_w = 18000 * (m_w & 65535) + (m_w >> 16);
    return (m_z << 16) + m_w;  /* 32-bit result */
}