1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
|
static UINT uiRealRand; //in BSS
void MySRand(void)
{
__asm
{
push edx
rdtsc //in edx:eax
mov uiRealRand,eax
pop edx
}
srand(uiRealRand);
}
UINT MyRand(void)
{
int iLibRand = rand(); //only 15bit
__asm
{
push ecx
mov ecx,iLibRand
and ecx,0xFF
or cl,1 //loop overflow
mov eax,uiRealRand //full 32bit
m1_: rcl eax,cl
add eax,uiRealRand
loop m1_
mov ecx,iLibRand
and ecx,0xFF00
xchg ch,cl
or cl,1
m2_: rcl eax,cl
add eax,uiRealRand
loop m2_
mov uiRealRand,eax
pop ecx
}
return uiRealRand;
}
UINT MyGetRandRange(UINT Min, UINT Max)
{
return ( (MyRand() % ((Max + 1)- Min) ) + Min);
// return ( (rand() % ((Max + 1)- Min) ) + Min); // stdlib
}
|