Created
August 28, 2018 18:45
-
-
Save matejthetree/5c510babe767634b6c5189d9e482aca3 to your computer and use it in GitHub Desktop.
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
/** | |
* Random Generator Library | |
*/ | |
const MersenneTwister = require("mersennetwister"); | |
const rndFunc = new MersenneTwister(); | |
// const rndFunc = new MersenneTwister(-5489); | |
// const rndFunc = Math.random(); | |
/** | |
* Randomize (shuffle) JS Array. | |
* @param {Array} arr -array to be shuffled | |
* @param {Function} rndFunc -random function: MarsenneTwister, ... | |
* @return {Array} -shuffled array | |
*/ | |
module.exports.shuffleArray = arr => { | |
let i; | |
let j; | |
let temp; | |
for (i = arr.length - 1; i > 0; i -= 1) { | |
j = Math.floor(rndFunc.random() * (i + 1)); | |
temp = arr[i]; | |
arr[i] = arr[j]; | |
arr[j] = temp; | |
} | |
return arr; | |
}; | |
/** | |
* MarsenneTwister Random Numbers (https://github.com/pigulla/mersennetwister) | |
* Filter amount of numbers from 'arr' using Marsennetwister random generator. | |
* @param {Array} arr -array of numbers (or strings) to be filtered | |
* @param {Number} amount -amount of numbers (or strings) to be selected from 'arr' | |
* @return {Array} -array of randomly selected numbers (or strings) | |
*/ | |
module.exports.filterMT = (amount, arr) => { | |
const arrShuffled = module.exports.shuffleArray(arr, rndFunc); | |
// console.log(JSON.stringify(arrShuffled)); | |
const start = 0; | |
const end = amount + start; | |
return arrShuffled.slice(start, end); // return part of arrShuffled array from start to end index | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment