Skip to content

Commit 620585a

Browse files
committed
Added array shuffle method
1 parent 2e722b0 commit 620585a

1 file changed

Lines changed: 24 additions & 0 deletions

File tree

src/math/random-data-generator/RandomDataGenerator.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,30 @@ var RandomDataGenerator = new Class({
449449
}
450450

451451
return [ '!rnd', this.c, this.s0, this.s1, this.s2 ].join(',');
452+
},
453+
454+
/**
455+
* A standard array shuffle implementation using the current seed.
456+
*
457+
* @method Phaser.Math.RandomDataGenerator#shuffle
458+
* @since 3.4.0
459+
*
460+
* @param {array[]} [array] - The array to be shuffled.
461+
*
462+
* @return {array} The shuffled array.
463+
*/
464+
shuffle: function (array)
465+
{
466+
var len = array.length - 1;
467+
for (var i = len; i > 0; i--)
468+
{
469+
var randomIndex = this.integerInRange(0, len);
470+
var itemAtIndex = array[randomIndex];
471+
472+
array[randomIndex] = array[i];
473+
array[i] = itemAtIndex;
474+
}
475+
return array;
452476
}
453477

454478
});

0 commit comments

Comments
 (0)