     dup -18 shift bitxor ; inline
 
 : next-index  ( mt -- i )
-    dup i>> dup mt-n < [ nip ] [ drop mt-generate 0 ] if ; inline
+    dup i>> dup n < [ nip ] [ drop mt-generate 0 ] if ; inline
 
 PRIVATE>
 


M: mersenne-twister random-32* ( mt -- r )
    [ next-index ]
    [ seq>> wrap-nth mt-temper ]
    [ [ 1+ ] change-i drop ] tri ;

USE: init


TUPLE: mersenne-twister { seq uint-array } { i fixnum } ;

: n 624 ; inline
: m 397 ; inline
: a uint-array{ 0 HEX: 9908b0df } ; inline

: y ( n seq -- y )
    [ nth-unsafe 31 mask-bit ]
    [ [ 1+ ] [ nth-unsafe ] bi* 31 bits ] 2bi bitor ; inline

: mt[k] ( offset n seq -- )
    [
        [ [ + ] dip nth-unsafe ]
        [ y [ 2/ ] [ 1 bitand a nth ] bi bitxor ] 2bi
        bitxor
    ] 2keep set-nth-unsafe ; inline

: mt-generate ( mt -- )
    [
        seq>>
        [ [ n m - ] dip '[ [ m ] dip _ mt[k] ] each ]
        [ [ m 1- ] dip '[ [ m n - ] [ n m - + ] bi* _ mt[k] ] each ]
        bi
    ] [ 0 >>i drop ] bi ; inline

: init-mt-formula ( i seq -- f(seq[i]) )
    dupd nth dup -30 shift bitxor 1812433253 * + 1+ 32 bits ; inline

: init-mt-rest ( seq -- )
    n 1- swap '[
        _ [ init-mt-formula ] [ [ 1+ ] dip set-nth ] 2bi
    ] each ; inline

: init-mt-seq ( seed -- seq )
    32 bits n <uint-array>
    [ set-first ] [ init-mt-rest ] [ ] tri ; inline

: mt-temper ( y -- yt )
