in reply to Re^2: Mersenne Twister
in thread Mersenne Twister

May I ask why in particular you're interested in the Mersenne Twister? I mean, they do claim that the Mersenne Twister is the "most popular" pseudorandom number generator today, but... the C library, for example, (and any program such as Perl that uses the standard C library) uses the Lehmer random number generator. It's very simple:

SEED = ( SEED * A + B ) MOD C

...where A, B, and C are some fractions, although in the C library, the value of B is set to 1.

Look here https://blogs.mathworks.com/cleve/2015/04/17/random-number-generator-mersenne-twister/ This page has a well documented source code. And maybe here as well https://en.wikipedia.org/wiki/List_of_random_number_generators which shows a list of random number generators.

Replies are listed 'Best First'.
Re^4: Mersenne Twister
by swl (Prior) on Jan 05, 2024 at 23:42 UTC

    The Mersenne Twister is a popular pseudo-random number generator (PRNG) because it has good distribution properties and a long period length (2^19937 - 1) before the sequence repeats. It passes all the diehard tests of randomness, and nearly all of the TestUO1 tests. (Wikipedia is a reasonable source of info for this: https://en.wikipedia.org/wiki/Mersenne_Twister).

    The algorithm you describe is a Linear Congruential Generator (LCG). These have global correlation structures that cause issues at the square root or cubed root of the period length.

    Perl's rand() uses the drand48 algorithm on all operating systems since 5.20 (https://metacpan.org/release/RJBS/perl-5.20.0/view/pod/perldelta.pod#rand-now-uses-a-consistent-random-number-generator). This has a longer period length (2^48) than the standard ANSI C rand (2^31) but is still an LCG and thus has issues with correlation structures. Some useful illustrations are at https://wellington.pm.org/archive/200704/randomness/index.html, for example slide7, slide8 and slide 14.

    Whether all the above matters depends on the application. If it is just selecting from a small pool of items a small number of times then the choice of PRNG is unlikely to be an issue. Applications with larger data sets require better quality PRNGs.

      "since 5.20"

      Hmm... Interesting. That's good to know... I have been using TinyPerl 5.8 mostly. When plotting random lines, I did notice that there is a visible difference or should I say "resemblance of a pattern" visible with what you mentioned, LCG, but when I plotted lines with Mersenne Twister, the lines appeared completely random.