Clear
Rand(-1)
Public gnRandCounter
gnRandCounter = 0
Local lnT0, lnReccount
lnReccount=0
t0 = Seconds()
Randomize("tmp",500,500)
lnReccount = lnReccount + Reccount("tmp")
Randomize("tmp",500,500)
lnReccount = lnReccount + Reccount("tmp")
Randomize("tmp",500,500)
lnReccount = lnReccount + Reccount("tmp")
Randomize("tmp",500,500)
lnReccount = lnReccount + Reccount("tmp")
Randomize("tmp",500,500)
lnReccount = lnReccount + Reccount("tmp")
? gnRandCounter," random numbers generated for "+Transform(lnReccount)+" records using your code."
? Seconds()-t0," seconds needed overall."
lnReccount=0
gnRandCounter = 0
t0 = Seconds()
Randomize2("tmp",500,500)
lnReccount = lnReccount + Reccount("tmp")
Randomize2("tmp",500,500)
lnReccount = lnReccount + Reccount("tmp")
Randomize2("tmp",500,500)
lnReccount = lnReccount + Reccount("tmp")
Randomize2("tmp",500,500)
lnReccount = lnReccount + Reccount("tmp")
Randomize2("tmp",500,500)
lnReccount = lnReccount + Reccount("tmp")
? gnRandCounter," random numbers generated for "+Transform(lnReccount)+" records using Mike's code."
? Seconds()-t0," seconds needed overall."
lnReccount=0
gnRandCounter = 0
t0 = Seconds()
Randomize("tmp",5,30)
lnReccount = lnReccount + Reccount("tmp")
Randomize("tmp",5,30)
lnReccount = lnReccount + Reccount("tmp")
Randomize("tmp",5,30)
lnReccount = lnReccount + Reccount("tmp")
Randomize("tmp",5,30)
lnReccount = lnReccount + Reccount("tmp")
Randomize("tmp",5,30)
lnReccount = lnReccount + Reccount("tmp")
? gnRandCounter," random numbers generated for "+Transform(lnReccount)+" records using your code."
? Seconds()-t0," seconds needed overall."
lnReccount=0
gnRandCounter = 0
t0 = Seconds()
Randomize2("tmp",5,30)
lnReccount = lnReccount + Reccount("tmp")
Randomize2("tmp",5,30)
lnReccount = lnReccount + Reccount("tmp")
Randomize2("tmp",5,30)
lnReccount = lnReccount + Reccount("tmp")
Randomize2("tmp",5,30)
lnReccount = lnReccount + Reccount("tmp")
Randomize2("tmp",5,30)
lnReccount = lnReccount + Reccount("tmp")
? gnRandCounter," random numbers generated for "+Transform(lnReccount)+" records using Mike's code."
? Seconds()-t0," seconds needed overall."
Browse Nowait
Procedure myrand()
gnRandCounter = gnRandCounter + 1
Return Rand()
Procedure Randomize()
Lparameters tcAlias, tnCount, tnMax
tnMax = Evl(tnMax,30)
Local lcNormalErrHand, lnCount, lnRetries, lnT0
If tnCount>tnMax
Error "tnCount can't be higher than tnMax"
Endif
* If tnCount>tnMax/2
* Error "Warning: Only use for tnCount much smaller than tnMax"
* Endif
Create Cursor (tcAlias) (irand I)
Index On irand Tag crand Candidate
Set Order To
lcNormalErrHand = On("Error")
On Error Retry
For lnCount=1 To tnCount
lnT0 = Seconds()
lnRetries = gnRandCounter
Insert Into (tcAlias) Values (myrand()*tnMax+1)
lnDuration = Seconds()-lnT0
lnRetries = gnRandCounter-lnRetries
If lnDuration>0.1 Or lnRetries>100
? "It took "+Transform(lnDuration)+" sec. and "+Transform(lnRetries)+" retries to get the "+Transform(lnCount)+"th number"
Endif
Next
If Empty(lcNormalErrHand)
On Error
Else
On Error &lcNormalErrHand
Endif
Endproc
Procedure Randomize2()
Lparameters tcAlias, tnCount, tnMax
tnMax = Evl(tnMax,30)
Create Cursor crsRandomizer (irand I, randorder B)
Index On randorder Tag Main
For lnI = 1 To tnMax
Insert Into crsRandomizer (irand, randorder) Values (lnI, myrand())
Endfor
Select Top tnCount irand From crsRandomizer Order By randorder Into Cursor (tcAlias)
Use In crsRandomizer
Endproc