RAND very slow

BlitzMax Forums/BlitzMax Programming/RAND very slow

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

this is faster. depends on what you are trying to do but an array of pre-generated random numbers will always win the day.

Local RandArray:Int[] = New Int[10000]

For x = 0 To 9999
	RandArray[x] = Rand(0,255)
Next



t1=MilliSecs()
For i=1 To 2000000
rc:+1 If rc>9999 Then rc=0
pos=RandArray[rc]
Print pos
'pos=(pos+7) Mod 256
Next
t2=MilliSecs()
Print t2-t1


this is slightly faster
t1=MilliSecs()
For i=1 To 2000000
pos=BillRand(0,255)
'pos=(pos+7) Mod 256
Next
t2=MilliSecs()
Print t2-t1

Function BillRand( min_value,max_value=1 )
	Local delta!=Float(max_value)-Float(min_value)
	If delta>0 Return RndFloat()*(1+delta)+min_value
	Return RndFloat()*(1-delta)+max_value
End Function