0

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[50]
      }
      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
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Ari Pili
  • 21
  • 1
  • 2
    If we're talking about O(f(N)) behavior, what exactly is `N` here for? – Severin Pappadeux Jun 20 '20 at 18:37
  • And once you decided that, it seems quite trivial to me. – gnasher729 Jun 21 '20 at 09:53
  • @gnasher729 so, what the best you can do? – Ari Pili Jun 21 '20 at 12:00
  • @SeverinPappadeux are you ask to me? now i very confuse with this problem and you ask the hell to me. Thanks – Ari Pili Jun 21 '20 at 12:02
  • What I'm saying, usually when people talk about `O(f(N))` behavior, they mean processing of sequence of `N` items, and how algorithm behave when `N` is very large and going to infinity. If f(N)~N people say this is linear algorithm, if f(N)~N^2 people say this is linear algorithm and so on and so forth. What is `N` in your case? Generating one single random number is a fixed set of calculations, so obviously f(N)~1 and you have O(1) algorithm. – Severin Pappadeux Jun 21 '20 at 15:24
  • correction: if f(N)~N^2 people say this is quadratic algorithm – Severin Pappadeux Jun 21 '20 at 19:11

0 Answers0