Skip to content

Instantly share code, notes, and snippets.

@yuki-takeichi
Created May 31, 2017 04:54
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 yuki-takeichi/8a69ea881dd989a0fa7bb0e6aa170c09 to your computer and use it in GitHub Desktop.
Save yuki-takeichi/8a69ea881dd989a0fa7bb0e6aa170c09 to your computer and use it in GitHub Desktop.
なるべく同時実行されないようにrandomにdelayさせる
const tries = 10000;
const trial = 30;
// in ms
const consumption = 30;
const concurrency = 3;
//const delay = 19185; // 50%ぐらい in 20 conc
//const delay = 4000; // 50%ぐらい in 10 conc
//const delay = 10000; // 5%ぐらい in 5 conc
//const delay = 1500; // 11%ぐらい in 3 conc
const delay = 500; // 30%ぐらい in 3 conc
const overlap = (delay1, delay2) => delay1 < delay2 + consumption && delay2 < delay1 + consumption;
const hoge = () => {
const times = new Array(concurrency).fill(null).map(() => Math.random() * delay);
return times.every((t1, i) => (times.every((t2, j) => i === j || !overlap(t1,t2))));
};
const avg = nums => {
const sum = nums.reduce((x, y) => x + y);
return sum / nums.length;
};
console.log(avg(new Array(trial).fill(null).map(() =>
new Array(tries).fill(null).map(() => hoge()).filter(x => x).length / (tries * 1.0)
)// 正規分布になる。平均が中央値。
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment