-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from tmcw/shuffle
Implement Fisher-Yates shuffle for random sub-selections
- Loading branch information
Showing
5 changed files
with
99 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
{ | ||
"indent": 4, | ||
"undef": true, | ||
"unused": true | ||
"unused": true, | ||
"globals": { | ||
"require": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
var test = require('tape'); | ||
var Random = require('random-js'); | ||
var random = new Random(Random.engines.mt19937().seed(0)); | ||
var ss = require('../'); | ||
|
||
function rng() { return random.real(0, 1); } | ||
|
||
test('shuffle', function(t) { | ||
var input = [1, 2, 3, 4, 5, 6]; | ||
t.deepEqual(ss.shuffle([], rng), []); | ||
t.deepEqual(ss.shuffle(input, rng), [1, 5, 3, 2, 4, 6]); | ||
t.deepEqual(input, [1, 2, 3, 4, 5, 6], 'does not change original array'); | ||
t.deepEqual(ss.shuffle(input, rng), [5, 4, 1, 3, 6, 2]); | ||
t.deepEqual(input, [1, 2, 3, 4, 5, 6], 'does not change original array'); | ||
t.end(); | ||
}); | ||
|
||
test('shuffle_in_place', function(t) { | ||
var input = [1, 2, 3, 4, 5, 6]; | ||
t.deepEqual(ss.shuffle_in_place([], rng), []); | ||
t.deepEqual(ss.shuffle_in_place(input, rng), [6, 1, 5, 2, 4, 3]); | ||
t.deepEqual(input, [6, 1, 5, 2, 4, 3], 'changes original array'); | ||
t.end(); | ||
}); |