#Best way to grab random values from a array without repeating any?

1 messages · Page 1 of 1 (latest)

worldly swift
#

The upgrades id's in my game are all set in a array, I'm trying to get 4 random upgrades from this array without repeating any of them, what would be the best approach?

lone fjord
#
choices = [1, 2, 3, 4, 5];
choose_unique = function() {
    var _n = array_length(choices);
    if (_n == 0) {
        return undefined;    
    }
    
    var _index = irandom(_n - 1);
    var _choice = choices[_index];
    array_delete(choices, _index, 1);
    
    return _choice;
};
#

Not sure if you wanna remove them from the array though.

worldly swift