Old 08-07-2013, 05:57 PM
drc2016 drc2016 is offline
Tech Curious
Join Date: Aug 2013
Posts: 1
Default Fisher-Yates Shuffle - Tasker Version

Fisher-Yates is one of the fastest, most random methods for shuffling an array of items in programming. For more info on the algorithm itself, and why it's good, click here. I have set up a Tasker version that can be called from another task to shuffle an array for you.

Here is the task:

FY Shuffle (7)
	A1: Variable Set [ Name:%array To:%par(1) Do Maths:Off Append:Off ] 
	A2: Variable Split [ Name:%array Splitter:, Delete Base:Off ] 
	A3: Variable Set [ Name:%index To:%array(#) Do Maths:Off Append:Off ] 
	A4: Variable Randomize [ Name:%rand Min:1 Max:%index ] 
	A5: Variable Set [ Name:%temp To:%array(%index) Do Maths:Off Append:Off ] 
	A6: Variable Set [ Name:%array(%index) To:%array(%rand) Do Maths:Off Append:Off ] 
	A7: Variable Set [ Name:%array(%rand) To:%temp Do Maths:Off Append:Off ] 
	A8: Variable Set [ Name:%index To:%index-1 Do Maths:On Append:Off ] 
	A9: Goto [ Type:Action Number Number:4 Label: ] If [ %index > 1 ]
	A10: Return [ Value:%array() Stop:On ]
Here is an example of another task using it:

FY Test (46)
	A1: For [ Variable:%count Items:1:50 ] 
	A2: Variable Set [ Name:%test(%count) To:%count Do Maths:Off Append:Off ] 
	A3: End For 
	A4: Perform Task [ Name:FY Shuffle Stop:Off Priority:8 Parameter 1 (%par1):%test() Parameter 2 (%par2): Return Value Variable:%return ] 
	A5: Wait Until [ MS:0 Seconds:1 Minutes:0 Hours:0 Days:0 ] If [ %return Is Set ]
	A6: Variable Split [ Name:%return Splitter:, Delete Base:Off ] 
	A7: Flash [ Text:%return() Long:Off ]
So this just creates a new array containing the numbers 1-50 in order, then passes that array (in the format %test(), as you can't actually pass an array to another task, just a single variable) to the FY shuffle task. The shuffle task then splits the now-comma-separated variable back into an array, shuffles it, and then returns the result, again in the %array() format.

This task, meanwhile, waits until the return variable, %return, is set before continuing, then splits the return variable back into an array once more, and it's done. I added the flash action simply as a way to test that everything was working.
Reply With Quote

algorithm, fisher, yates

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -10. The time now is 12:01 AM.

Powered by vBulletin®
Back to Top Copyright © 2006-2016 CrowdGather. All rights reserved. About Pocketables | Advertisers | Investors | Legal | Contact