hi,

I don't know which API is responsible for random number generation. Is there such an API function? My main purpose is generation random number between 40h-60h.


regards
Posted on 2005-02-12 13:16:34 by highenergy
you can use GetTickCount.

but i have coded a simple Rountine for RandomNumber generation.

RandomChar.asm


;written by diablo2oo2
;example:
;invoke RandomChar,chr$("ABCDEF0123456789") ;returns one of this char in eax

RandomChar PROTO :DWORD

RAND32 PROTO :DWORD
InitRand32 PROTO

.code
RandomChar proc _string:DWORD
LOCAL _retvalue :BYTE
pushad

mov esi,_string
;---get lenght of string---
xor ecx,ecx
.while byte ptr[esi+ecx]!=0
inc ecx
.endw
;---get random char---
invoke InitRand32
invoke RAND32,eax
movzx eax,ax
cdq
idiv ecx
mov al,byte ptr[esi+edx]
mov _retvalue,al
popad

movzx eax,_retvalue
ret
RandomChar endp

;-----------------------------------------------------------------------------
; ----random number generator by NaN---
; ---(modified a bit to make it a procedure by Exagone)---

.data?
NaNRand dd ?
.code

SWAP MACRO M1:REQ, M2:REQ
xor M1, M2
xor M2, M1
xor M1, M2
ENDM

InitRand32 proc
db 0fh,31h
shr eax, 2
add eax, 1
mov NaNRand, eax
ret
InitRand32 endp

RAND32 proc base:DWORD
; Random number generator based on the Real time clock
; and the Park, Miller random number algorithm
;
; Coded by NaN for WIN32ASM
; May 5, 2001
; rev 2.
push ecx
push edx


mov eax, NaNRand
mov edx,0
mov ecx, 127773 ;q
div ecx ; eax == floor( seed / q)
; edx == remainder
SWAP eax, edx
push edx
mov ecx, 16807
mul ecx ; eax = mul of remainder * a
pop edx ; edx == floor of seed/q

SWAP eax, edx
push edx
mov ecx, 2836
mul ecx
pop edx ; edx == mull of rem * a
; eax == mull of seed/q * r

sub edx, eax
mov eax, edx
mov NaNRand, eax ; save next seed
mov ecx, base
mov edx, 0
div ecx
mov eax, edx
pop edx
pop ecx
ret
RAND32 endp
Posted on 2005-02-12 14:00:07 by diablo2oo2
Rand proc ;; :-D
LOCAL hProv,Rnd:byte
invoke CryptAcquireContext,addr hProv,0,0,1,0
invoke CryptGenRandom,hProv,1,addr Rnd
invoke CryptReleaseContext,hProv,0
movzx eax,Rnd
ret
Rand endp

but i think
rdtsc
and eax,(60h-40h)-1 ;; eax mod 20h
would suffice
Posted on 2005-02-12 16:33:45 by drizz
Thanks for replies. :oops:
Posted on 2005-02-13 00:59:05 by highenergy
hey, that my proc is not what you want...

to do best, lookup rand in masm32lib, or use diablo's code or search the board
Posted on 2005-02-13 06:20:42 by drizz