A use for #PB_ANY

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

A use for #PB_ANY

Post 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:

Code: Select all

SomeNumber.l = #PB_ANY
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
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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
quidquid Latine dictum sit altum videtur
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post 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.
oldefoxx
Enthusiast
Enthusiast
Posts: 532
Joined: Fri Jul 25, 2003 11:24 pm

Random means Redundant is an equal possibility

Post 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.
has-been wanna-be (You may not agree with what I say, but it will make you think).
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Re: Random means Redundant is an equal possibility

Post 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...
fweil
Enthusiast
Enthusiast
Posts: 725
Joined: Thu Apr 22, 2004 5:56 pm
Location: France
Contact:

Post 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.
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.
Shannara
Addict
Addict
Posts: 1808
Joined: Thu Oct 30, 2003 11:19 pm
Location: Emerald Cove, Unformed

Post 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.
oldefoxx
Enthusiast
Enthusiast
Posts: 532
Joined: Fri Jul 25, 2003 11:24 pm

Post 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.
has-been wanna-be (You may not agree with what I say, but it will make you think).
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6166
Joined: Sat May 17, 2003 11:31 am
Contact:

Post by blueznl »

entirely right, you could go fancy and free and reuse numbers if you want
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply