Chainsaw 9 Posted September 15, 2017 Report Share Posted September 15, 2017 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! Quote Link to post Share on other sites
HelloInsomnia 1103 Posted September 15, 2017 Report Share Posted September 15, 2017 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)) } Quote Link to post Share on other sites
Chainsaw 9 Posted September 15, 2017 Author Report Share Posted September 15, 2017 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") Quote Link to post Share on other sites
Chainsaw 9 Posted September 15, 2017 Author Report Share Posted September 15, 2017 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") Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.