# What algorithm does the Shuffle method implement?

What algorithm does the Shuffle method implement?

How is the Shuffle seeded?

feedback://showreport?report_id=33140

Possibly the FisherYates shuffle (aka Knuth shuffle)?

You might have to re-post this in Pro to make sure the engineers see it. Curious why it matters though.

[quote=79293:@Justin Elliott]What algorithm does the Shuffle method implement?

How is the Shuffle seeded?

I wouldn’t rely on Array.Shuffle for anything past a trivial “I want this shuffled in some way but don’t care how”.

I debated on posting it there, but wasn’t sure if the threads in the Xojo Pro channel would show up in web search results, which I favor.

The reason I want to know what algorithm is implemented is that if it’s not a “strong” one that I’d prefer, then I need to write my own implementation of Knuth or something else.

If you’re trying to get a result that “feels random” (a problem Apple ran into with the original “shuffle songs” feature), you might run Knuth a couple times, then run a few iterations of checking for things that are “too next to each other” and manually swapping with random entries. Up to you to decide what “too next to each other” means.

I think I see the problem. You are trying to shuffle your tracks so that a song from artist X does not play too close to another track from artist X, is that it?

I wrote an AppleScript for this years ago, and you’re welcome to it. I use it for my family’s Christmas party playlist, and it makes sure that a song does not appear within 3 tracks of another song with a similar name (an issue when you have 6 version of “White Christmas”) or same artist. I’m sure it can be adapted to Xojo easily.

Fisher Yates is dead simple - esp the computing implementation of it
From http://en.wikipedia.org/wiki/FisherYates_shuffle

``` for i from n ? 1 downto 1 do j ? random integer with 0 ? j ? i exchange a[j] and a[i]```
Ours implements this algorithm but you cannot control the seed so IF that is important you can very easily implement your own with some other randomizing mechanism

thanks Norm.

Thanks to Paul Lefebvre the Xojo docs have been updated with the algorithm and seed questions:

http://documentation.xojo.com/index.php/Shuffle