Simple Array Shuffle

2

Attached Files

The following files have been attached to this tutorial:

.PNG

construct-shuffle.PNG

Download now 14.88 KB

Stats

6,219 visits, 9,963 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Published on 1 Jul, 2013. Last updated 19 Feb, 2019

Here's how to shuffle an array in 1 event. In this example, I fill a simple array with 4 values (0, 1, 2, 3). This represents 4 music tracks in which I want to make a shuffled playlist.

Setup:

Create 2 Arrays with the same Width (in this case 4)

1 as a temporary Array (Array_Temp)

1 as the final shuffled Array (Array_Music)

Create a Local variable (pick = 0)

     Event: System | Repeat 4 times
     Action: Array_Temp | Set value at loopindex to loopindex

(the above event is used to simply fill our empty array with values)

     Event: System | Repeat 4 times
     Action: System | set pick to floor(random(Array_Temp.Width))
     Action: Array_Music | Set value at loopindex to Array_Temp.At(pick)
     Action: Delete index pick from X axis

In a nutshell, "pick" selects a random position in the temp array and copies that value into next sequential position in the shuffled array. Then it removes the array position in the temp array ensuring it's never chosen again.

.PNG

construct-shuffle.png

Download now 14.88 KB
  • 1 Comments

  • Order by
Want to leave a comment? Login or Register an account!
  • I'm not sure if this is correct. When you delete array element, you just make it "empty". The "pick" (random 1-4) can still eventually select this unexisting index. How can you make to NOT select again already picked index from the Array_Temp?