[Development] CSPRNG vs DPRNG

Matthew Woehlke mwoehlke.floss at gmail.com
Thu Oct 12 18:23:13 CEST 2017


On 2017-10-11 12:12, Thiago Macieira wrote:
> I created a better option: QPseudoRandomGenerator (name bikeshedding later) 
> on the flight home from QtCS. It's a simple wrapper around the Mersenne Twister 
> provided by the Standard C++ Library

The last time I found myself in need of a graphic/game quality PRNG, I
tried fiddling with both rand48 (POSIX) and C++11 MT, and ended up using
this instead:

 m_seed = ( ( 19073486328125 * m_seed ) + 1 ) & 0x7fffffffffffffff;
 return ldexp( static_cast< double >( m_seed ), -63 );

While I am not an expert in RNG's, from the bit of reading I did around
the time, it was not obvious to me how MT is better than a well crafted LCG.

According to my notes, "empirical testing indicates that [the above] is
about 30% faster than mt19937_64". I suppose, however, it's possible
that the improvement has something to do with my final output being
[0..1) real numbers rather than integers.

(I don't recall any more where I got the constants for that particular
LCG, but I believe that particular LCG is known as a "good"
implementation. At any rate, I'm not the only person to use that
particular LCG.)

> But it has a hard requirement on std::mt19937 (see other email).

The above has zero dependencies :-). (Other than the architecture being
able to perform 64-bit integer math. But if you can't do that, you're
probably better off accepting the quality loss and using a 32-bit LCG.)

-- 
Matthew




More information about the Development mailing list