Add the random method for the array object:
(function () {
function Random_sn (IArray) {
Return Math.floor (Math.random () * iarray.length);
}
function Probability_random (IArray) {
var random_int;
if (iarray.random_sn = = undefined)
IARRAY.RANDOM_SN =-1;
Do random_int = RANDOM_SN (IArray);
while (Random_int = = IARRAY.RANDOM_SN)
IARRAY.RANDOM_SN = Random_int;
return Iarray[random_int];
}
function Sequence_random (IArray) {
Return Iarray.splice (RANDOM_SN (IArray), 1) [0];
}
Array.prototype.random = function (Mode, Pop) {
if (! Mode)
Return Probability_random (this);
if (! POP) {
if (! (This. Random_queue && this. Random_queue.length))
This. Random_queue = [].concat (this);
Return Sequence_random (this. Random_queue);
}
Return Sequence_random (this);
};
})();
Use examples:
"Random Probability"
//
The return of elements is completely random, the probability of occurrence is uncertain, and a finite number of calls is not guaranteed to return all elements
var ielement = Iarray.random ();
"Sequential random"
//
The return of the element is periodic, and the elements appear once in each cycle, but in a sequential order
var ielement = Iarray.random (true);
"Random Bubbling"
//
Each call takes a random fetch of an element from the original array and returns (the original array changes in place)
var ielement = Iarray.random (True, true);