Skip to content

Instantly share code, notes, and snippets.

@todays-mitsui
Last active March 20, 2018 02: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 todays-mitsui/a78589a0e67a11b91011328b28869349 to your computer and use it in GitHub Desktop.
Save todays-mitsui/a78589a0e67a11b91011328b28869349 to your computer and use it in GitHub Desktop.
JavaScript で、指定した範囲の整数をランダムに返す関数
/**
* 指定した範囲の整数をランダムに取得する
*
* @param {number} min ランダムに取得したい整数の下限
* @param {number} max ランダムに取得したい整数の上限
* @returns {number} min 以上 max 以下の整数
*/
function randomInt(min, max) {
var interval = max - min + 1;
return ~~(Math.random() * interval + min);
}
{
const min = 0;
const max = 13;
const n = 1000000;
// 集計バケツの生成と初期化
const bucket = Object.create(null);
for (let i = min; i <= max ; i++) {
bucket[i] = 0;
}
// 試運転
for (let j = 0; j < n; j++) {
bucket[randomInt(min, max)]++;
}
console.table(bucket);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment