dwyl / dwyl/learn-javascript

Random Number Generator "Randomness" Check?

オープン
#21 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
enhancement question
主要言語
HTML
スター
65
フォーク
15
PR マージ指標
30日以内にマージされた PR はありません

説明

Given the following "Random Integer between range X and Y" function:

``` js
/**
* Returns a random integer between min (inclusive) and max (inclusive)
* Using Math.round() will give you a non-uniform distribution!
* from: http://stackoverflow.com/a/1527820/1148249
*/
function get_random_int(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
```

We can run the following test to confirm the randomness:

``` js
var i = 0; // counter for looping.
var results = {}; // store the results of running get_random_int
var min = 0;
var max = 9;
for(i = min; i <= max; i++) {
results[i] = 0; // initialize results to zero;
}
i = 0; // reset/reuse i counter.
var test_count = 10000000;
while(i < test_count) {
var r = get_random_int(min, max);
results[r]++; // increment the count for a given random number
i++;
}
Object.keys(results).forEach(function(k) {
results[k] = results[k].toString() + ' > ' + (results[k] / test_count * 100).toFixed(3) + ' %';
});
console.log(JSON.stringify(results, null, 2));
```

Which outputs:

``` js
{
"0": "1000900 > 10.009 %",
"1": "999727 > 9.997 %",
"2": "999742 > 9.997 %",
"3": "999268 > 9.993 %",
"4": "1000650 > 10.007 %",
"5": "998621 > 9.986 %",
"6": "1000241 > 10.002 %",
"7": "1000716 > 10.007 %",
"8": "1000151 > 10.002 %",
"9": "999984 > 10.000 %"
}
```

Which is a pretty decent distribution...

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

調査の方向性

まずリポジトリ内で get_random_int 関数を見つけ、issue に示されている JavaScript ループを、指定された範囲とテスト回数で実行します。issue ではファイル、テストの場所、要求されているリポジトリの変更が特定されていないため、完了の定義を決める前に、分布チェックを追加するのか文書化するのかを明確にします。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
javascript
領域
testing
issue の種類
機能追加
難易度
3/5
見積もり時間
1〜2日
活発さ
停滞
明瞭さ
説明が足りない
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。