- A simple example of how you could go about shuffling a group of things.
- The exact same thing, just drawn differently, to transition into...
- Basically the exact same thing again, just with a slight difference that doesn't affect anything.
- The algorithm above written in Lua.
- An example run through.
What you could do is pick a random shape, and place it in the area on the left.
Let's say we picked the circle. Let's move it to the other side!
And now we can just repeat. Pick a random shape, and move it to the other side.
Let's pick the pentagon.
And now the square.
And, well, we don't have a choice, let's move the triangle over too.
And that's it! Shuffled.
Okay, now for the same thing, except without all the blank spaces.
Let's pick a random shape. We'll pick the same shapes as last time, so let's pick the circle.
And then, move it over to the left.
So now we pick another random shape from the area on the right. I'll put a red flag to show where the area on the right starts.
Let's pick the pentagon.
And now the square.
And hey, turns out the last shape is already there.
So, that example was the same thing as the one before. If that wasn't clear, here's both of them side by side:
Okay, now this time it'll be ever-so-slightly different. Instead shifting across the shapes on the right when one of them is placed in the area on the left, we'll swap them. You'll see.
So let's do this again, and pick a random shape, which will be the circle.
Now let's move it to the area on the left, swapping it with the triangle under the flag.
This is basically the same position we were in with the other examples after one move, except the order of the shapes in the area on the right is different. The order of the shapes on the right doesn't matter though, since we'll be picking one from there at random anyway.
Now let's pick another random shape from the area on the right, the pentagon, and swap it with whatever is under the flag, which is itself. Not a problem!
Now let's pick another random shape from the area on the right, the square, and swap it with the triangle under the flag.
And there's only one left, and it's already in the only place it can be, so we're done!
So how could we write this algorithm in English? How about...
Start with the flag at the first element.
Pick a random element between the flag and the last element.
Swap it with the element at the flag.
Move the flag along
Repeat the last three steps, stopping when the flag is at last element.
Let me conveniently rephrase that...
Loop, with the flag starting at the first element and moving along every time, and when the flag is at one-before-the-last-element this will be the final iteration, and do the following things inside the loop:
- Pick a random element between the flag and the last element
- Swap it with the element at the flag
Now here's that in Lua:
Let's see how this works with a run through!
So let's say we give this function our table of shapes...
"i" is the flag! It starts at 1, and loops through 1-less-than-the-number-of-elements times, which is 3 in this case.
Let's see what happens on the first iteration of the loop...
And the second...
And the third and final iteration...
And the table has been shuffled!