Code: Select all
Debug $FFFFFFFF ; Returns 4294967295 (not negative)
Debug Random($FFFFFFFF) ; "Can't be negative" error
Code: Select all
Debug $FFFFFFFF ; Returns 4294967295 (not negative)
Debug Random($FFFFFFFF) ; "Can't be negative" error
Code: Select all
Procedure.i Random32bit()
ProcedureReturn (Random($FFFF) << 16) | Random($FFFF)
EndProcedure
For i = 1 To 10
Debug Hex(Random32bit(), #PB_Long)
Next i
Code: Select all
Define q.q
RandomData(@q, SizeOf(Quad))
Debug q
Code: Select all
Procedure.q RandomQuad(Max.q = $7FFFFFFFFFFFFFFF)
Protected Quad.q, Limit.q
Protected MaxPlusOne.q = Max+1
If Max <= 0
ProcedureReturn 0
EndIf
RandomData(@Quad, 8)
Quad & $7FFFFFFFFFFFFFFF ; remove the sign bit
If MaxPlusOne & Max = 0
ProcedureReturn Quad & Max ; no balancing needed when Max = 2^n-1
EndIf
Limit = $7FFFFFFFFFFFFFFF - $7FFFFFFFFFFFFFFF % MaxPlusOne - 1
While Quad > Limit ; balancing needed, generate a new number
RandomData(@Quad, 8)
Quad & $7FFFFFFFFFFFFFFF ; remove the sign bit
Wend
ProcedureReturn Quad % MaxPlusOne ; reduce to the range [0, Max]
EndProcedure
Debug RandomQuad(1000000000000)