Random Dice (Two RNG examples + distribution tests)
Posted: Thu Jun 11, 2009 4:07 am
Here are two ways to "throw" 5 random dice.
Oh and if anyone knows how to do mod in asm (safely) I'd love to hear it.
It's not that the LCG_Dice() procedure below with the mixed asm and PureBasic is slow, but I can't help but feel that asm could make it even faster.
Enjoy folks, Public Domain (as my posts always are, unless stated otherwise)
Oh and if anyone knows how to do mod in asm (safely) I'd love to hear it.
It's not that the LCG_Dice() procedure below with the mixed asm and PureBasic is slow, but I can't help but feel that asm could make it even faster.
Enjoy folks, Public Domain (as my posts always are, unless stated otherwise)
Code: Select all
DisableDebugger
Procedure.i RandomSource()
!RDTSC ;Read the CPU Time Stamp Counter
ProcedureReturn ;Return the EAX register content, (32bit)
EndProcedure ;we're not using the upper half (EDX) as that hardly changes at all.
Procedure.l LCG_Dice(*seed.Long)
Protected bit.l,random.l,bias.l,i.l
random=*seed\l
!mov eax,dword[p.v_random]
!mov ecx,1664525
!mul ecx
!add eax,7
!mov dword[p.v_random],eax
bit=(random%6)
If bit<0
bit+6
EndIf
bit+1
*seed\l=random
ProcedureReturn bit
EndProcedure
Define seed.l,r.l,d1.l,d2.l,d3.l,d4.l,d5.l,d6.l
seed=RandomSource()
d1=0
d2=0
d3=0
d4=0
d5=0
d6=0
For i=1 To 60000000
r=LCG_Dice(@seed)
Select r
Case 1
d1+1
Case 2
d2+1
Case 3
d3+1
Case 4
d4+1
Case 5
d5+1
Case 6
d6+1
EndSelect
Next
EnableDebugger
Debug d1
Debug d2
Debug d3
Debug d4
Debug d5
Debug d6
Debug ""
DisableDebugger
RandomSeed(seed)
d1=0
d2=0
d3=0
d4=0
d5=0
d6=0
For i=1 To 60000000
r=Random(5)+1
Select r
Case 1
d1+1
Case 2
d2+1
Case 3
d3+1
Case 4
d4+1
Case 5
d5+1
Case 6
d6+1
EndSelect
Next
EnableDebugger
Debug d1
Debug d2
Debug d3
Debug d4
Debug d5
Debug d6