...,
Don't know if this may really help in the meanwhile but here is a code that allows to allocate unique numbers in a given range.
BTW, you may use this for generating gadgets numbers with a rather low memory consumption, either if this method uses a certain amount of memory.
By using a 64K bytes array, it is not so much expensive, and this works fine and fast if you tune the parameters accordingly to your application requirements.
Code: Select all
#AnyUniqueRange = $10000
#AnyUniqueFailed = -1
Dim AnyUniqueArray.b(#AnyUniqueRange)
Procedure.l AnyUnique()
Result.l = Random(#AnyUniqueRange)
EndLess.l = Result - 1
If AnyUniqueArray(Result) = #FALSE
AnyUniqueArray(Result) = #TRUE
Else
While AnyUniqueArray(Result) And Result <> EndLess
Result + 1
If Result > #AnyUniqueRange
Result = 0
EndIf
Wend
EndIf
If Result = EndLess
ProcedureReturn #AnyUniqueFailed
Else
ProcedureReturn Result
EndIf
EndProcedure
;
; Test
;
For i = 1 To 1000
x = AnyUnique()
Debug x
Next
The only work this does is to manage a True/False byte for each possible value of the randomly generated index. Unfortunately no bit array exists, so I use a byte array.
When a random index is generated, the procedure checks if the position is free (#FALSE) or not (#TRUE). If such, a collision method is applied to increment the index until a free place has been found. Incrementation is reset to zero when the index reaches the end of the array and so on ...
Only when no more place is available in the array, the procedure returns -1.
Such a way should help to answer more or less to use it easily in usual apps design.
My avatar is a small copy of the 4x1.8m image I created and exposed at 'Le salon international du meuble à Paris' january 2004 in Matt Sindall's 'Shades' designers exhibition. The original laminated print was designed using a 150 dpi printout.