Skip to content

Instantly share code, notes, and snippets.

@mqklin
Created April 9, 2021 09:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mqklin/5a35f46017ea2ec67def0841f91c1f23 to your computer and use it in GitHub Desktop.
Save mqklin/5a35f46017ea2ec67def0841f91c1f23 to your computer and use it in GitHub Desktop.
get random performance
function getRandoms() {
const examples = 100;
for (var i = 0; i < examples; i++) {
const arr = Array(getRandomInt(1, 6)).fill(null);
for (var j = 0; j < arr.length; j++) {
arr[j] = getRandomInt(-50, 150);
}
console.log(`[${arr.map(a => a + '%').join(', ')}] => `, perf(arr).toFixed(2) + '%');
}
}
function perf(arr) {
var x = Math.sqrt(arr.slice(1).reduce(
(acc, perf) => {
return acc * (1 + perf / 100)
},
1 + arr[0] / 100
));
return 1000 * (x / (1 + x) - 0.5);
}
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min + 1)) + min;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment