Advertisement
Awn_pastebin

CommaProb.j

Jan 23rd, 2015
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. /*
  3.  
  4. Nレスでコンマゾロ目の種類がどれくらい出るかその割合をシミュレートする    
  5.  
  6. *Note that:
  7.     ブラウザへの負荷(?)を低減するべく、計算結果はconsole.log()上にのみ出力します
  8.  
  9. *出力例
  10. ------------------------------
  11.       【テスト概要】    
  12. テスト回数: 10,000,000回
  13. 1テスト中のレス数: 1000レス
  14.      【テスト結果】    
  15. ゾロ目の種類の数
  16. 0個,1個,2個,3個,4個,5個,6個,7個,8個,9個,10個
  17. 出現回数
  18. 474,7453,58945,271217,826788,1714867,2462263,2414209,1552369,591533,99882
  19. 出現頻度
  20. 0.004%,0.074%,0.589%,2.712%,8.267%,17.148%,24.622%,24.142%,15.523%,5.915%,0.998%
  21. ------------------------------
  22. */
  23.  
  24. ///////////////////////////////
  25. //
  26. //パラメータ
  27. //
  28.  
  29.  
  30. //テスト回数
  31. testKaisu = 10000000;
  32.  
  33. //1テスト中のレス数
  34. N = 1000;
  35.  
  36.  
  37. //
  38. //
  39. ///////////////////////////////
  40.  
  41.  
  42.  
  43. //乱数発生用の関数
  44. function getRandomInt(min, max) {
  45.   return Math.floor( Math.random() * (max - min + 1) ) + min;
  46. }
  47.  
  48. //コンマゾロ目の種類が揃った数を保持するための変数
  49. bunpu = [0,0,0,0,0,0,0,0,0,0,0];
  50.  
  51.  
  52. //シミュレーターの作成
  53. function mySimulator(){
  54.     output  = [];
  55. //  output2 = [];
  56.  
  57.     for(i = 0; i < N; i++){
  58.         //乱数発生
  59.         w = getRandomInt(0,999);
  60.  
  61.         //コンマがゾロ目の時
  62.         if(w % 111 == 0){
  63.             if(output.indexOf(w) == -1){
  64.                 output.push(w);
  65. //              output2.push(i);
  66.             }
  67.         }
  68.     }
  69. //  console.log(output.length);
  70.     return output.length;
  71. }
  72.  
  73.  
  74.  
  75.  
  76. //シミュレーションを"testKaisu"回実行する
  77. for(j = 0; j < testKaisu; j++){
  78. //  console.log((j + 1) + "回目のテストです");
  79.  
  80.     //シミュレーターの実行、戻り値を得る
  81.     r = mySimulator();
  82. //  console.log(r);
  83.  
  84.     //ゾロ目が何種類揃ったか、その分布を取る
  85.     bunpu[r] += 1;
  86.  
  87. //結果確認
  88. /*
  89.     console.log(output.join(","));
  90.     console.log(output2.join(","));
  91. */
  92. }
  93.  
  94.  
  95. //【出力】見出し
  96.     console.log("     【テスト概要】     ");
  97.     console.log("テスト回数: " + String(testKaisu).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, '$1,') + "回");
  98.     console.log("1テスト中のレス数: " + N + "レス");
  99.     console.log("     【テスト結果】     ");
  100.  
  101. //【出力】結果
  102.     console.log("ゾロ目の種類の数");
  103.     console.log("0個,1個,2個,3個,4個,5個,6個,7個,8個,9個,10個");
  104.     console.log("出現回数");
  105.     console.log(bunpu.join(","));
  106.     console.log("出現頻度");
  107.     console.log(( bunpu.map(function(x) {return Math.floor(100 * 1000 * (x / testKaisu)) / 1000 + "%" } ) ).join(","));//小数点第3桁まで出す
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116. //End Of Script
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement