mirror of
https://github.com/dmuth/diceware.git
synced 2024-11-23 17:26:34 -05:00
Added demonstrateBias() function for testing purposes.
This commit is contained in:
parent
336c7cd375
commit
f4dd2b1e79
1 changed files with 38 additions and 0 deletions
38
tests/lib.js
Normal file
38
tests/lib.js
Normal file
|
@ -0,0 +1,38 @@
|
|||
|
||||
/**
|
||||
* Return an array with max values, where each value is the number of times
|
||||
* the remainer was that of the index. This is used to show bias in a
|
||||
* particular dividend.
|
||||
*
|
||||
* Based on code at https://github.com/dmuth/diceware/issues/7
|
||||
*/
|
||||
function demonstrateBias(max) {
|
||||
|
||||
var map = new Array(max);
|
||||
|
||||
for (var i = 0; i < max; i++) {
|
||||
map[i] = 0;
|
||||
}
|
||||
|
||||
//
|
||||
// Go through all possible values (note that any power of 2
|
||||
// should work, instead of 256...) and increment map by one for each
|
||||
// remainder we get across that set.
|
||||
//
|
||||
for (var i = 0; i < 256; i++) {
|
||||
map[i % max]++;
|
||||
}
|
||||
|
||||
//
|
||||
// All of the values in this should be equal. If not, there's bias!
|
||||
//
|
||||
return map;
|
||||
|
||||
} // End of demonstrateBias()
|
||||
|
||||
|
||||
//console.log(demonstrateBias(4));
|
||||
//console.log(demonstrateBias(5));
|
||||
//console.log(demonstrateBias(6));
|
||||
//console.log(demonstrateBias(8));
|
||||
|
Loading…
Reference in a new issue