NateTut has asked for the wisdom of the Perl Monks concerning the following question:

Anyone have a non-obfuscated (antiobfuscated?) pure perl Mersenne Twister implementation? I have decided to play in perl in my richly deserved retirement. Thanks!

Replies are listed 'Best First'.
Re: Mersenne Twister
by marto (Cardinal) on Jan 05, 2024 at 16:37 UTC
      I guess I was hoping for something a little less obfuscated. My perl is a little rusty... I'd settle for some good pseudocode.

        The module source hasn't been obfuscated, perhaps you're looking for some pseudocode for the algorithm? Your search engine of choice can help you out there.

        ”…pseudocode…”

        Stolen from here. If it’s good is another question.

        // Create a length n array to store the state of the generator int[0..n-1] MT int index := n+1 const int lower_mask = (1 << r) - 1 // That is, the binary number of +r 1's const int upper_mask = lowest w bits of (not lower_mask) // Initialize the generator from a seed function seed_mt(int seed) { index := n MT[0] := seed for i from 1 to (n - 1) { // loop over each element MT[i] := lowest w bits of (f * (MT[i-1] xor (MT[i-1] >> (w-2) +)) + i) } } // Extract a tempered value based on MT[index] // calling twist() every n numbers function extract_number() { if index >= n { if index > n { error "Generator was never seeded" // Alternatively, seed with constant value; 5489 is used in + reference C code[54] } twist() } int y := MT[index] y := y xor ((y >> u) and d) y := y xor ((y << s) and b) y := y xor ((y << t) and c) y := y xor (y >> l) index := index + 1 return lowest w bits of (y) } // Generate the next n values from the series x_i function twist() { for i from 0 to (n-1) { int x := (MT[i] and upper_mask) | (MT[(i+1) mod n] and lower_mask) int xA := x >> 1 if (x mod 2) != 0 { // lowest bit of x is 1 xA := xA xor a } MT[i] := MT[(i + m) mod n] xor xA } index := 0 }

        «The Crux of the Biscuit is the Apostrophe»