Jump to content
UBot Underground

Shuffle A List - Looking For A Simple Way To Shuffle A List In Ubot


Recommended Posts

I'm looking for a simple way to shuffle a list in Ubot.

 

What I'm wanting to do is randomly pick from a big list of hashtags, without choosing the same hashtag more than once.

 

Seems the easiest solution would be to shuffle the hashtag list and then grab the first x number of tags.

 

Perhaps someone has already done this before or perhaps there is a better way to do this.

 

Thanks!

Link to post
Share on other sites

Easy way - use the File Management plugin:

clear list(%numbers)
set(#number,1,"Global")
loop(10) {
    add item to list(%numbers,#number,"Don\'t Delete","Global")
    increment(#number)
}
clear list(%shuffled)
add list to list(%shuffled,$plugin function("File Management.dll", "$shuffle list", %numbers),"Delete","Global")

I know some have had trouble downloading his plugins lately so if you can't then try this function instead:

clear list(%numbers)
set(#number,1,"Global")
loop(15) {
    add item to list(%numbers,#number,"Don\'t Delete","Global")
    increment(#number)
}
clear list(%shuffled)
add list to list(%shuffled,$list from text($ShuffleList($text from list(%numbers,$new line)),$new line),"Delete","Global")
define $ShuffleList(#_listToShuffle) {
    add list to list(%_listToShuffle,$list from text(#_listToShuffle,$new line),"Don\'t Delete","Local")
    set(#_shuffled,"false","Local")
    loop while($comparison(#_shuffled,"=","false")) {
        if($comparison($list total(%_listToShuffle),">",0)) {
            then {
                set(#_listToShuffleTotal,$list total(%_listToShuffle),"Local")
                set(#_listToShuffleItem,$subtract($rand(1,#_listToShuffleTotal),1),"Local")
                add item to list(%_shuffled,$list item(%_listToShuffle,#_listToShuffleItem),"Don\'t Delete","Local")
                remove from list(%_listToShuffle,#_listToShuffleItem)
            }
            else {
                set(#_shuffled,"true","Local")
            }
        }
    }
    return($text from list(%_shuffled,$new line))
}
Link to post
Share on other sites

Thanks HelloInsomnia, that is awesome!

 

I've been working on a solution using Javascript and this is what I've come up with.

set(#hashtags,"a,b,c,d,e","Global")
set(#hashtags,$eval("function shuffleTags(tags) \{
   var a = tags.split(\',\');
   var j, x, i;
   for (i = a.length; i; i--) \{
       j = Math.floor(Math.random() * i);
       x = a[i - 1];
       a[i - 1] = a[j];
       a[j] = x;
   \}
   return a.join();
\}
shuffleTags(\"{#hashtags}\");"),"Global")
Link to post
Share on other sites

Here's an update on the Javascript version putting it into a Ubot Function including a parameter to set the number of random hashtags to return.

set(#hashtags,"a,b,c,d,e","Global")
define $TagRandomizer(#hashtags, #maxtags) {
    return($eval("
       function shuffleTags(tags) \{
          var a = tags.split(\',\');
          var j, x, i;
          for (i = a.length; i; i--) \{
              j = Math.floor(Math.random() * i);
              x = a[i - 1];
              a[i - 1] = a[j];
              a[j] = x;
          \}
          return a.slice(0,{#maxtags}).join();
       \}
       shuffleTags(\"{#hashtags}\");
    "))
}
set(#randomtags,$TagRandomizer(#hashtags, 3),"Global")

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...