News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

Mersenne Twister

Started by donkey, October 09, 2008, 06:15:19 AM

Previous topic - Next topic

donkey

I needed a pseudo-random number generator today for a project and rather than go through a stack of archive disks for a translation of the mersenne twister I wrote a few years back I decided to try downloading it from the forum. Well, it must of ended up in the old forum and is no longer available so I went through my archives and thought I would repost it here. This translation is posted with the permission of Makoto Matsumoto, co-author of the mersenne twister algorithm...

;Makoto Matsumoto and Takuji Nishimura
;psuedorandom number generating algorithm
;copyright 1997, matumoto@math.keio.ac.jp
;http://www.math.keio.ac.jp/~matumoto/emt.htmlcode

.CONST

N equ 624
M equ 397
MN4 equ -908
TEMPERING_MASK_B equ 9d2c5680h
TEMPERING_MASK_C equ 0efc60000h
UM equ 80000000h
LM equ 7fffffffh

.DATA
MTI dd (N+1)
MC dd 69069
MATRIX dd 0
dd 9908b0dfh
MT dd 2496 dup (?)

.CODE

Randomize FRAME Seed
pushad
lea edi,MT
mov eax,[Seed]
mov [edi],eax
mov ecx,N
add edi,4
:
mul D[MC]
stosd
dec ecx
jnz <
mov D[MTI],N
popad
ret
ENDF

RandM FRAME limit
push 0
lea edi,MT
cmp D[MTI],N
jb >>L1
cmp D[MTI],N+1
jnz >L2
rdtsc
push eax ; Generate a new seed
call Randomize
L2:
mov esi,edi
L3:
mov eax,[esi]
and eax,UM
mov ebx,[esi+4]
and ebx,LM
or eax,ebx
mov ecx,eax
shr eax,1
mov edx,esi
add edx,(M*4)
mov ebx,[edx]
xor eax,ebx
and ecx,1
xor eax,[MATRIX+ecx*4]
mov [esi],eax
add esi,4
inc D[esp]
cmp D[esp],(N-M)
jnz <L3
L4:
mov eax,[esi]
and eax,UM
mov ebx,[esi+4]
and ebx,LM
or eax,ebx
mov ecx,eax
shr eax,1
mov edx,esi
add edx,MN4
mov ebx,[edx]
xor eax,ebx
and ecx,1
xor eax,[MATRIX+ecx*4]
mov [esi],eax
add esi,4
inc D[esp]
cmp D[esp],(N-1)
jnz <L4
mov edx,edi
add edx,(M-1)*4
mov ebx,[edx]
xor eax,ebx
and ecx,1
xor eax,[MATRIX+ecx*4]
mov [esi],eax
mov D[MTI],0
L1:
mov esi,edi
mov eax,[MTI]
inc D[MTI]
shl eax,2
add esi,eax
mov eax,[esi]
mov ebx,eax
shr eax,11
xor ebx,eax
mov eax,ebx
shl eax,7
and eax,TEMPERING_MASK_B
xor ebx,eax
mov eax,ebx
shl eax,15
and eax,TEMPERING_MASK_C
xor ebx,eax
mov eax,ebx
shr eax,18
xor eax,ebx
xor edx,edx
div D[limit]
mov eax,edx
pop ebx
ret
ENDF


Donkey
"Ahhh, what an awful dream. Ones and zeroes everywhere...[shudder] and I thought I saw a two." -- Bender
"It was just a dream, Bender. There's no such thing as two". -- Fry
-- Futurama

Donkey's Stable