Skip to content

Commit

Permalink
add shuffle to random module
Browse files Browse the repository at this point in the history
  • Loading branch information
bnmnetp committed Jan 10, 2013
1 parent 71ca096 commit 2fdf4d1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dist/builtin.js

Large diffs are not rendered by default.

20 changes: 18 additions & 2 deletions src/lib/random/__init__.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,17 @@ MersenneTwister.prototype.genrand_res53 = function() {

/* These real versions are due to Isaku Wada, 2002/01/09 added */


function fisherYates ( myArray ) {
var i = myArray.length, j, tempi, tempj;
if ( i == 0 ) return false;
while ( --i ) {
j = Math.floor( Math.random() * ( i + 1 ) );
tempi = myArray[i];
tempj = myArray[j];
myArray[i] = tempj;
myArray[j] = tempi;
}
}

var $builtinmodule = function(name)
{
Expand Down Expand Up @@ -235,5 +245,11 @@ var $builtinmodule = function(name)
high = high - 1;
return Math.round(myGenerator.genrand_res53()*(high-low))+low;
});

mod.shuffle = new Sk.builtin.func(function(myarray) {
fisherYates(myarray.v)
});

return mod;
}

}

0 comments on commit 2fdf4d1

Please sign in to comment.