Jump to content
UBot Underground

Help With Picking Random List Item By Last Time Used


Recommended Posts

I have a list in ubot that I also store as a table (because there is no random table cell command).

The 0 column of the table is the same as the list.

The 1 column of the table is the TimeStamp it was last used.

I use the $random list item to find a random item in the list

Then I find its list position and store that in a variable #pos

Here comes the part I can't figure out (and I may be way over thinking this).

All I want to do is say something like this:

 

IF table cell #pos,1 is empty (never been used) OR hasn't been used today (preferably a more precise measure like x minutes) 

Then save table cell #pos,0 to a variable and update table cell #pos,1 with $now (current timestamp since we are about to use it)

Else find a new random list entry and try again

 

If it makes it through the entire table and can't find any that are available, clear column 1 and start again

 

 

Link to post
Share on other sites

Hello,

Here is another look at it. I hope I understood you correctly.

save to file("{$special folder("Application")}\\myTable.csv","one,
two,some date
three,
four,")
clear table(&myTable)
create table from file("{$special folder("Application")}\\myTable.csv",&myTable)
clear list(%random cells)
set(#myTable Index,0,"Global")
loop($table total rows(&myTable)) {
    if($comparison($table cell(&myTable,#myTable Index,1),"= Equals",$nothing)) {
        then {
            add item to list(%random cells,#myTable Index,"Don\'t Delete","Global")
        }
    }
    increment(#myTable Index)
}
set table cell(&myTable,$random list item(%random cells),1,"now")

Regards,
CD

get-set empty table cell.ubot

Link to post
Share on other sites

That only accounts for entries where the last time used is blank ($nothing).

The OR part of that was look at the current date/time and compare it to the stored one and only pick it if the date is blank OR time diff at least X minutes.

I want to use things from the list randomly, but only so many times per given time frame.

Say for instance a list of proxies. I want to choose one randomly (so there is no pattern of use in the server logs I'm visiting), but don't want to use the same proxy unless the last time it was used was more than say 5 minutes ago (that time frame can change based on application).

Then once I find one that meets the criteria, I can store the current timestamp to the table so next loop it can't pick that one again.

The rest is fairly simple, if none of the entries meets the above criteria, then they must all be used up. Here i can simply reset all the times to null or loop until one becomes available.

Link to post
Share on other sites
  • 1 month later...

Here is the solution I came up with. When "WorkToDo" can't find any records that haven't been used,  For this example I just had it end the script, in my actual script I have it reset all records "Used" flag (Table column 2) to false, so next time it runs the whole list is available again.

&myTable contains 3 columns for this example.

0=Name,1=UsedFlag(true or false),2=TimeStamp of last use

You'll have to make you sample csv if want to test it out.

Something like this:

one,false,05/24/2018 13:27:29

two,false,05/24/2018 11:50:36
three,true,05/24/2018 13:31:54
four,false,05/24/2018 11:47:58
clear table(&myTable)
create table from file("{$special folder("Application")}\\myTable.csv",&myTable)
clear list(%random cells)
set(#TotalRows,$subtract($table total rows(&myTable),1),"Global")
set(#WorkToDo,$plugin function("TableCommands.dll", "$table search", &myTable, "false", "Row Index"),"Global")
if(#WorkToDo < 0) {
    then {
        stop script
    }
}
set(#LoopCount,0,"Global")
set(#ExitLoop,0,"Global")
loop while(#ExitLoop = 0) {
    set(#myTable Index,$rand(0,#TotalRows),"Global")
    set(#datediff,$n ago($n seconds(12,"Hours")),"Global")
    if($either($comparison($table cell(&myTable,#myTable Index,2),"= Equals",$nothing),$comparison($table cell(&myTable,#myTable Index,2),"<",#datediff))) {
        then {
            add item to list(%random cells,#myTable Index,"Don\'t Delete","Global")
            set table cell(&myTable,$random list item(%random cells),2,$now)
            set table cell(&myTable,$random list item(%random cells),1,"true")
            set(#ExitLoop,1,"Global")
        }
    }
    increment(#LoopCount)
    if(#LoopCount > #TotalRows) {
        then {
            set(#LoopCount,0,"Global")
        }
    }
}
save to file("{$special folder("Application")}\\myTable.csv",&myTable)

Edited by Denethor
Link to post
Share on other sites

the line:

set(#TotalRows,$subtract($table total rows(&myTable),1),"Global")

Fixes a problem I had with the $rand command.

I originally had set the index to be simply a random number between 0 and $table total rows(&myTable)

That however does not work because $table total rows(&myTable) does not account for tables starting at position 0, so it would randomly create an empty data set at the end of the file with nothing but the true,$now

 

The rest should be pretty self explanatory, if anyone has questions, feel free to ask.

Edited by Denethor
Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...