need a random value from table

Find quick help here to get you started with Hollywood
Post Reply
peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

need a random value from table

Post by peceha »

Hi,
lest's say I have a table (not a list):

Code: Select all

t={[0]=0,[10]=10,[20]=20,[30]=30}
and I'd like to get some random value from it.
So far I'm using following code:

Code: Select all

Local rand=Rnd(TableItems(t))
Local b	= -1
For i,v In Pairs(t)
	b = b + 1
	If b = rand
		DebugPrint(t[i])
		Break
	EndIf
Next
Is there a faster way to get it done?

Thanks
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: need a random value from table

Post by Bugala »

Yes there is, depending how your code works.

You have a table:

Code: Select all

table = {
         ["item1"] = 10,
         ["item2"] = 5,
         ["item3"] = 20,
         ...
          }
But question is, is the amount of "itemx" the same, or does it keep changing during the program too?

Suppose it is stable, then you can have two different tables that keep track of same thing:

Code: Select all

Table = {like above}
Helptable = { "item1", "item2", "item3"}
Now you can do following:

Code: Select all

Rand = Rnd( TableItems(helptable) )
key = helptable[Rand]
debugprint( Table[key] )
and you are getting the value you are after.


If amount of "itemx" keep changing, then you need to remember to update that helptable as well. Good thing however is that if you are looking for random value among all the "item"s, then you dont need to worry that table and helptable have them in same order, as it doesnt really matter which "itemx" it happens to pick as long as it is random and exists. And do remember, if you remove one "itemx" from your table, you need to remove that from helptable as well, or you might be trying to access non existing table item.
peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

Re: need a random value from table

Post by peceha »

Thanks for the idea.
That table is changing - it is getting smaller and smaller until it becomes null.

I asked that question because I'm trying to speed up my program as much as possible on classic amiga (that "get random" routine is used in main loop of the script).
I am going to try your solution and will report if that speeds up things

Thanks
User avatar
TheMartian
Posts: 109
Joined: Sun Feb 28, 2010 12:51 pm

Re: need a random value from table

Post by TheMartian »

Hi

I think you may be able to simplify the process even further without a help table by using RemoveItem() like this...


mytbl = {10, 5, 20}

Function GetRndValue()
Local response
rand=Rnd(TableItems(mytbl))
response=mytbl[rand]
RemoveItem(mytbl,rand)
Return(response)
EndFunction


Now each call to the function will pick a value using a random value among the existing indexes (which implicitly goes between 0 and the number of items-1). Then remove that item with RemoveItem() and you will have a 'rebuilt' and reduced table since Hollywood automatically handles the indexing with implicitly assigned indexes.

Of course you will eventually run out of items in the list as you remove them. So you have to be able to handle this condition in your code.

regards
Jesper
peceha
Posts: 111
Joined: Tue Dec 13, 2016 8:39 am
Location: Poland

Re: need a random value from table

Post by peceha »

I think that is not going to work in my case:

Code: Select all

mytbl={ [0]="zero" , [1]="one" , [6]="six" }
following line will give me a number between 0 and 2

Code: Select all

rand=Rnd(TableItems(mytbl))
but position no.2 does not exist thus this

Code: Select all

response=mytbl[rand]
will throw an error if RAND=2
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: need a random value from table

Post by airsoftsoftwair »

I don't think there's a faster way than your original code based on iterating the table until you reach the random index. There's no way to go from an absolute, consecutive index to a non-consecutive index without iterating through the table.
User avatar
TheMartian
Posts: 109
Joined: Sun Feb 28, 2010 12:51 pm

Re: need a random value from table

Post by TheMartian »

Hi

The procedure I suggest will work provided the value you are interested in is the content (which might be a number or a string) and not its index in an table. As I understand your task, you have a list of values
and wish to grab a random item in the list for some purpose, then reduce the list by removing that item, then later grab another random item, then reduce the list by removing that item... etc.

In other words, in your situation the actual value of the index in the table is not important, which is why you can use the implicit indexing (consecutive indexes). Because it is only used to do random picking among the items. If however you actually use the index value as a parameter in some other context, then my idea is not useful.

regards
Jesper
Post Reply