deltarho[1859] have a look and see what you think
Code: Select all
Function sfc32_rangedrnd_noasm(Byval dwlowerbound As Ulong, Byval dwrange As Ulong, Byref dw_a As Ulong, Byref dw_b As Ulong, Byref dw_c As Ulong, Byref dw_counter As Ulong) As Long
'sfc32 (small fast counting) prng by chris doty-humphrey, creator of practrand. (pcg-random.org/posts/bob-jenkins-small-prng-passes-practrand.html (bottom of post)
' > pracrand.sourceforge.net > download practrand > practrand-pre0.95 > src > rngs > sfc.)
'macroenableet
dw_counter+=1
Static dw_tmp As Ulong
dw_tmp = dw_a + dw_b + dw_counter
Static dwshifted As Ulong
'dwshifted = dw_b
dwshifted = dw_b Shr 9
dw_a = dw_b Xor dwshifted
dwshifted = dw_c
dwshifted Shl= 3
dw_b = dw_c + dwshifted
dwshifted = dw_c
Asm
rol [dwshifted], 21
End Asm
dw_c = dwshifted + dw_tmp
Function = Clng(Csng(dw_c) * (Csng(dwrange - 1) / 4294967296!)) + dwlowerbound
'macroerrortrap
End Function 'sfc32_rangedrnd_noasm()
#Define numresamplingtrials 20
Sub fbmain ()
'initialize sfc32.
Dim As Ulong dw_a, dw_b, dw_c, dw_counter
dw_a = 0
dw_counter = 1
dw_b = &h1115eed0
dw_c = &h1115eed1 '\/ == lowerbound, upperbound - lowerbounc + 1
Dim As Long funcresult
Do
sfc32_rangedrnd_noasm(1, 300, dw_a, dw_b, dw_c, dw_counter)
Loop Until dw_counter = 13 '"warm up" as per original source code.
'use sfc32 many times...
Dim As Long trialnum, myprn
For trialnum = 1 To numresamplingtrials
myprn = sfc32_rangedrnd_noasm(1, 300, dw_a, dw_b, dw_c, dw_counter)
Print myprn
Next trialnum
Print "press return to exit"
Sleep
End Sub
fbmain
I could not see any room for improvement in your excellent code, however I think it would be interesting to have a version with as little asm as possible, not only to compare performance but also to be able to compile to 64-bit