Last active
March 20, 2018 02:54
-
-
Save todays-mitsui/a78589a0e67a11b91011328b28869349 to your computer and use it in GitHub Desktop.
JavaScript で、指定した範囲の整数をランダムに返す関数
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
/** | |
* 指定した範囲の整数をランダムに取得する | |
* | |
* @param {number} min ランダムに取得したい整数の下限 | |
* @param {number} max ランダムに取得したい整数の上限 | |
* @returns {number} min 以上 max 以下の整数 | |
*/ | |
function randomInt(min, max) { | |
var interval = max - min + 1; | |
return ~~(Math.random() * interval + min); | |
} |
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
{ | |
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