Hi,
i've found somewhere the attached random gen.
I call it many times in a game loop so i need a random gen not perfect but if possible faster that this.

If some help. THX A
Posted on 2001-10-07 09:01:47 by Bit7
This code is based on the formula Random(j+1) = 16807 * Random(j) mod 2147483647

Maybe it's faster.



.DATA
_seed dword 1
.CODE

ENDIF

RandomInit MACRO seed
push seed
pop _seed
ENDM

Random MACRO

mov eax, _seed
mov ebx, 16807
mov ecx, 2147483647
mul ebx
div ecx
mov _seed, edx
mov eax, edx
ENDM
Posted on 2001-10-08 02:04:19 by Dr. Manhattan
angelo, the source you attached looks weird :P. The bit instructions
have traditionally been slow, but should be OK in ppro+ cores.
However the algo looks suboptimal (running in a loop and all).

Hmm, manhattan... mul & div... icky poo :).

Try messing with a "good seed", some rotate and adds. The random
characteristics will be pretty shitty, but they might be good enough.

This code works reasonably well (quick&dirty-ported from C, asm
version not tested yet).



mov eax, [seed]

mov ecx, eax
shl eax, 4
shr ecx, 3
add eax, ecx
xor eax, [seed]
add eax, 064374785h

mov [seed], eax
Posted on 2001-10-08 10:02:38 by f0dder
heres another...you get OK "random" results, dont use 2..4...8 etc as the ARG though...it doesnt like it

;uses eax,ebx,edx
;result in edx = 0-(ARG-1)
M_RANDOM MACRO ARG
mov eax,random_seed
lea eax, ;eax*9
mov random_seed,eax
and eax,7fffffffh
cdq ;convert eax to edx:eax
mov ebx,ARG
div ebx ;edx(remainder)=random number
ENDM

random_seed DD 1
Posted on 2001-10-08 17:16:06 by Lennon
;copyright 1997, matumoto@math.keio.ac.jp
;http://www.math.keio.ac.jp/matumoto/emt.html

Rand proto :DWORD ;Generates a number on [0,Max-1]
Randomize proto :DWORD ;Changes the seed for Rand

.const
N equ 624
M equ 397
MATRIX_A equ 9908b0dfh
TEMPERING_MASK_B equ 9d2c5680h
TEMPERING_MASK_C equ 0efc60000h
UM equ 80000000h
LM equ 7fffffffh

.data
mti dd (N+1)
MC dd 69069

.data?
mt dd 2496 dup(?)

.code
Randomize proc Seed:DWORD
lea edi,mt
mov eax,Seed
mov ,eax
mov mti,1
@R: add edi,4
mul MC
mov ,eax
inc mti
cmp mti,N
jnz @R
ret
Randomize endp

Rand proc Max:DWORD
LOCAL Counter:DWORD
lea edi,mt
.IF mti>=N
.IF mti==N+1
INVOKE Randomize,4357
.ENDIF
mov Counter,0
mov esi,edi
@S: mov eax,
and eax,UM
mov ebx,
and ebx,LM
or eax,ebx
mov ecx,eax
shr eax,1
mov edx,esi
add edx,(M*4)
mov ebx,
xor eax,ebx
and ecx,1
.IF ecx
xor eax,MATRIX_A
.ENDIF
mov ,eax
add esi,4
inc Counter
cmp Counter,(N-M)
jnz @S

@T: mov eax,
and eax,UM
mov ebx,
and ebx,LM
or eax,ebx
mov ecx,eax
shr eax,1
mov edx,esi
add edx,((M-N)*4)
mov ebx,
xor eax,ebx
and ecx,1
.IF ecx
xor eax,MATRIX_A
.ENDIF
mov ,eax
add esi,4
inc Counter
cmp Counter,(N-1)
jnz @T

mov edx,edi
add edx,(M-1)*4
mov ebx,
xor eax,ebx
and ecx,1
.IF ecx
xor eax,MATRIX_A
.ENDIF
mov ,eax
mov mti,0
.ENDIF
mov esi,edi
mov eax,mti
inc mti
shl eax,2
add esi,eax
mov eax,
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 Max
mov eax,edx
ret
Rand endp

----------------------------------------------------------

To use - Invoke Randomize,eax
Hope this helps

Nok.
Posted on 2001-10-09 13:09:15 by Nokturnal
I APOLOGIZE FOR MY DELAY ON ASNSWER :(
(too much job, now too much asm is waiting to be written @ my home !)

thanks all, now i will study all your generators. I'd like also undersrtand ...

Bye,

/\
/_\
/ \
Posted on 2001-10-12 16:33:13 by Bit7