Page 1 of 1
A use for #PB_ANY
Posted: Wed Apr 28, 2004 5:27 pm
by Shannara
Right now, I need to be able to return a number that is created by PB itself, from a procedure. I would use Rnd(Random) command, but the numbers it return can easily conflict with existing #PB_ANY numbers. Plus, it does not take into account numbers already used..
I want to be able to use #PB_ANY as a real constant. Something like the following:
That way, there will not be the same number twice, and the number would not conflict with any other numbers generated by #PB_ANY. I have already tried the above, and it errors out, so...
The following code also do not work:
Code: Select all
Structure Window
Width.l
EndStructure
NewList Windows.Window()
AddElement(Windows())
Windows()\Width = #PB_ANY
Posted: Wed Apr 28, 2004 5:44 pm
by freak
#PB_Any is just a numeric constant with the value -1
The object functions (like OpenWindow() and such) are the ones
that create the dynamic number when given #PB_Any as parameter,
not the compiler or anything else.
So what you ask is not possible.
Timo
Posted: Wed Apr 28, 2004 5:45 pm
by Shannara
Ok, then if someone could lock this, or consider this a close thread, I'll make a new one for a non repeating random number generator.
Random means Redundant is an equal possibility
Posted: Wed Apr 28, 2004 5:56 pm
by oldefoxx
I see several people at work on the idea of using a "random" and yet unique ID for objects, and I have to ask myself, what's the point?
Look. If you randomly sort a deck of cards after every deal, the next card turned up could easily have played in the hand before. If you "randomly" select what is suppose to be a unique ID number, then the chances are that you can still get back a number that you are currently using. That is because you have not employed any effort to define persistence of existing Unique IDs.
Just as continuing play with a deck of cards without shuffling means that no duplicate cards should be turned over (unless someone is cheating),
the card deck gives you that persistence. It means that as a card is removed from the pool of available cards, played, then placed in the discard pile, the pool of available cards (the deck) continues to shrink, until you are forced to consider a reshuffle.
By shuffling the cards beforehand, you do not delay play when it comes to selecting the next card from the deck. It's just there. Now if you have some specific reason why you feel that objects have to have random yet unique IDs (rather than merely unique IDsd), then consider the range of possible Unique ID values, create an array to reflect each one, and randomly sort them, then deal them out like a deck of cards.
I think the real problem is that some people have lumped the terms "random" and "unique" together, and they do not mean the same thing at all. If you don't understand the difference, then dig out the old dictionary or ask someone who knows. Instead, they are willy-nilly coming to the boards here and asking for something that is totally unnecessary and adds no benefit to the design and construction of a computer game or other application.
It's time to stop this thread, IMHO.
Re: Random means Redundant is an equal possibility
Posted: Wed Apr 28, 2004 6:01 pm
by Shannara
oldefoxx wrote:Instead, they are willy-nilly coming to the boards here and asking for something that is totally unnecessary and adds no benefit to the design and construction of a computer game or other application.
Hmm, you comment makes no sense if you have ever developed a computer game before. Therefore, I must assume that you have never developed a game in your life?
A unique number is required (or a GUID) for uniquely assigning a number or string combination to an object, giving it a unique name at programming time, instead of rely on the user (or in my case, programmer who uses my engine) to define such an object. This is quite usefull when loading and freeing object dynamically .... When you deal with dynamically loading and free multiple objects/sprites at a time, you will understand
But I do agree with you on one thing, It's time to stop this thread...
Posted: Wed Apr 28, 2004 6:23 pm
by fweil
...,
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.
Posted: Wed Apr 28, 2004 6:46 pm
by Shannara
Thanks, didnt mean to be snappy before, but it's a pretty standard need for game development.
Anyways, i'll give that one a gander. Thanks.
Posted: Wed Apr 28, 2004 7:18 pm
by oldefoxx
If you have used up 0 to N numbers, then N+1 represents a unique number not found in the range 0 to N. That makes it unique. Therefore it does not require any random process to find a unique number.
Posted: Wed Apr 28, 2004 7:41 pm
by blueznl
entirely right, you could go fancy and free and reuse numbers if you want