チョコエッグ購入モンテカルロシミュレーションのJavaScript

試行回数10,000回


<SCRIPT LANGUAGE="JavaScript">
<!--

function calc(){

// エッグの名前一覧
var eggname = new Array("001 柴犬 (赤)","002 柴犬 (黒)","003 シャム","004 ブンチョウ (桜)","005 ブンチョウ (白)","006 アメリカンショートヘア (シルバー)","007 アメリカンショートヘア (ブラウン)","008 ヒョウモントカゲモドキ","009 ウィペット (フォーン)","010 ウィペット (ブリンドル)","011 フェレット (セーブル)","012 フェレット (アルビノ)","013 セキセイインコ (ノーマル)","014 セキセイインコ (ブルー)","015 セキセイインコ (ルチノー)","016 ゴールデンハムスター (野生種)","017 ゴールデンハムスター (茶&白)","018 ゴールデンハムスター (ベージュ)","019 ランチュウ (黒)","020 ランチュウ (更紗)","021 ネザーランドドワーフ (黒)","022 ネザーランドドワーフ (グレー)","023 ネザーランドドワーフ (茶)","024 ゴールデンレトリバー","025 ダックスフンド (ブラック&タン)","026 ダックスフンド (レッド)","027 日本猫 (三毛)","028 日本猫 (黒白ぶち)","029 スコティッシュフォールド (トータシェル)","030 スコティッシュフォールド (ブラウン)","031 ダッチ","032 チャボ (白)","033 チャボ (ゴイシ)","034 ミドリガメ (ノーマル)","035 ミドリガメ (アルビノ)");

// 最大値・最小値の初期値
var max = 0;
var min = 10000;

// 購入個数配列
var mont = new Array;

// 最後に購入したエッグ配列
var lastegg = new Array;

// モンテカルロシミュレーションのためのループ
for (j=0; j<10000; j++) {

// エッグの初期値
var egg = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);

// 購入回数
var counter = 0;

// エッグが35種類集まったかどうかを判定

for (i=0; i<10000; i++) {

if ( (egg[0] == 0) || (egg[1] == 0) || (egg[2] == 0) || (egg[3] == 0) || (egg[4] == 0) || (egg[5] == 0) || (egg[6] == 0) || (egg[7] == 0) || (egg[8] == 0) || (egg[9] == 0) || (egg[10] == 0) || (egg[11] == 0) || (egg[12] == 0) || (egg[13] == 0) || (egg[14] == 0) || (egg[15] == 0) || (egg[16] == 0) || (egg[17] == 0) || (egg[18] == 0) || (egg[19] == 0) || (egg[20] == 0) || (egg[21] == 0) || (egg[22] == 0) || (egg[23] == 0) || (egg[24] == 0) || (egg[25] == 0) || (egg[26] == 0) || (egg[27] == 0) || (egg[28] == 0) || (egg[29] == 0) || (egg[30] == 0) || (egg[31] == 0) || (egg[32] == 0) || (egg[33] == 0) || (egg[34] == 0) ) {

// 0から34の乱数を発生
var m = 0; var n = eggname.length;
val = Math.floor((n-m)*Math.random()+m);

// 購入したエッグの個数を増加
egg[val] = egg[val] + 1;

// ループ回数をカウント
counter = counter + 1;

// if
}

// for
}

// 購入個数
mont[j] = counter;

// 最後に購入したエッグ
lastegg[j] = eggname[val];

// 最大値・最小値
if (counter > max) { max = counter; }
if (counter < min) { min = counter; }

// for
}

// 35種類集まったエッグの各個数を表示

var total = 0;
for (j=0; j<10000; j++) {
total = total + mont[j];
}

// 購入個数の平均
ave = eval(total/10000);

document.write("<h4>累計</h4>" + total + "個<br>\n");
document.write("<h4>平均</h4>" + ave + "個\n");

// 最大値・最小値
document.write("<h4>最大値</h4>" +  max + "個<br>\n");
document.write("<h4>最小値</h4>" +  min + "個<br>\n");

// 有効桁数を2桁にする (ヒストグラム作成に使用)
var intmax = Math.round(max/10)*10;
var intmin = Math.round(min/10)*10;

// ヒストグラムの初期値 0-9,10-19,…,390-399 の度数に対応
var histo = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);

// 有効桁数を2桁にして,度数をカウント
var intmont = new Array;
for (j=0; j<10000; j++) {
intmont[j] = Math.round(mont[j]/10)*10;
if (intmont[j] == 0) { histo[0] = histo[0] + 1; }
if (intmont[j] == 10) { histo[1] = histo[1] + 1; }
if (intmont[j] == 20) { histo[2] = histo[2] + 1; }
if (intmont[j] == 30) { histo[3] = histo[3] + 1; }
if (intmont[j] == 40) { histo[4] = histo[4] + 1; }
if (intmont[j] == 50) { histo[5] = histo[5] + 1; }
if (intmont[j] == 60) { histo[6] = histo[6] + 1; }
if (intmont[j] == 70) { histo[7] = histo[7] + 1; }
if (intmont[j] == 80) { histo[8] = histo[8] + 1; }
if (intmont[j] == 90) { histo[9] = histo[9] + 1; }
if (intmont[j] == 100) { histo[10] = histo[10] + 1; }
if (intmont[j] == 110) { histo[11] = histo[11] + 1; }
if (intmont[j] == 120) { histo[12] = histo[12] + 1; }
if (intmont[j] == 130) { histo[13] = histo[13] + 1; }
if (intmont[j] == 140) { histo[14] = histo[14] + 1; }
if (intmont[j] == 150) { histo[15] = histo[15] + 1; }
if (intmont[j] == 160) { histo[16] = histo[16] + 1; }
if (intmont[j] == 170) { histo[17] = histo[17] + 1; }
if (intmont[j] == 180) { histo[18] = histo[18] + 1; }
if (intmont[j] == 190) { histo[19] = histo[19] + 1; }
if (intmont[j] == 200) { histo[20] = histo[20] + 1; }
if (intmont[j] == 210) { histo[21] = histo[21] + 1; }
if (intmont[j] == 220) { histo[22] = histo[22] + 1; }
if (intmont[j] == 230) { histo[23] = histo[23] + 1; }
if (intmont[j] == 240) { histo[24] = histo[24] + 1; }
if (intmont[j] == 250) { histo[25] = histo[25] + 1; }
if (intmont[j] == 260) { histo[26] = histo[26] + 1; }
if (intmont[j] == 270) { histo[27] = histo[27] + 1; }
if (intmont[j] == 280) { histo[28] = histo[28] + 1; }
if (intmont[j] == 290) { histo[29] = histo[29] + 1; }
if (intmont[j] == 300) { histo[30] = histo[30] + 1; }
if (intmont[j] == 310) { histo[31] = histo[31] + 1; }
if (intmont[j] == 320) { histo[32] = histo[32] + 1; }
if (intmont[j] == 330) { histo[33] = histo[33] + 1; }
if (intmont[j] == 340) { histo[34] = histo[34] + 1; }
if (intmont[j] == 350) { histo[35] = histo[35] + 1; }
if (intmont[j] == 360) { histo[36] = histo[36] + 1; }
if (intmont[j] == 370) { histo[37] = histo[37] + 1; }
if (intmont[j] == 380) { histo[38] = histo[38] + 1; }
if (intmont[j] >= 390) { histo[39] = histo[39] + 1; }
}

// 度数を表示
document.write("<h4>ヒストグラム</h4>\n");
document.write("<table><tbody>\n");
document.write("<tr><td>クラス (n 個以上)</td><td>度数</td></tr>\n");

for (i=0; i<40; i++) {
var classNum = eval(i*10);
width = histo[i] * 1;
document.write("<tr><td>" + classNum + "- </td><td>" + histo[i] + "</td><td><img src=bar.gif width=" + width + " height=21></td></tr>\n");
}
document.write("</tbody></table>\n");

}
calc();

//-->
</SCRIPT>


2000-11-21