Skip to content

Instantly share code, notes, and snippets.

@heygleeson
Last active September 26, 2023 20:01
Show Gist options
  • Save heygleeson/bab8c68cbc27b216d92a5b596e2e23fa to your computer and use it in GitHub Desktop.
Save heygleeson/bab8c68cbc27b216d92a5b596e2e23fa to your computer and use it in GitHub Desktop.
array_shuffle() for GameMaker Studio 2.3.1
// Adds to GMS2.3.1's new "array_" functions.
// Based of Fisher-Yates Shuffle https://bost.ocks.org/mike/shuffle/
function array_shuffle(_array) {
var _len = array_length(_array), _last = 0, _i = 0;
while(_len) {
_i = irandom(--_len);
_last = _array[_len];
_array[_len] = _array[_i];
_array[_i] = _last;
}
return _array;
}
@plonp
Copy link

plonp commented Nov 8, 2022

Hey, thanks so much for this code! Took me a while to understand such simple concept.
If I am understanding this correctly, basically this function just swaps 2 array positions (_len array position & _i array position) for a number of times (array_length).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment