Guest User

Untitled

a guest
Jun 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using System.IO;
  5.  
  6. public class RandomTest : MonoBehaviour {
  7.  
  8. // サイコロの目を定義
  9. int[] dice = new int[] {1, 2, 3, 4, 5, 6};
  10.  
  11. // サイコロの目が出た回数を記録するDictionary
  12. Dictionary<int, int> diceDict;
  13. Dictionary<int, float> probDict;
  14.  
  15. // 1セットあたりの試行回数
  16. [SerializeField]
  17. int rollNum = 10000;
  18.  
  19. // セット数
  20. [SerializeField]
  21. int setNum = 1000;
  22.  
  23. void Start(){
  24. OutputDiceRollResults();
  25. }
  26.  
  27. void OutputDiceRollResults(){
  28. // 処理開始のメッセージ
  29. Debug.Log("***** 乱数の偏りの確認を開始 *****");
  30.  
  31. // CSVファイルのヘッダを出力
  32. StreamWriter sw;
  33. FileInfo fi;
  34. string fineName = Application.dataPath + "/Resources/Csv/dice_output.csv";
  35. fi = new FileInfo(fineName);
  36.  
  37. sw = fi.CreateText();
  38. sw.WriteLine(GetHeaderLineString());
  39.  
  40. for (int i = 0; i < setNum; i++){
  41. // 辞書を初期化する
  42. InitializeDiceDicts();
  43.  
  44. // サイコロを振る
  45. RollDicesWithNumber(rollNum);
  46.  
  47. // それぞれの目が出た回数を確率に直す
  48. SetProbDictFromDiceDict();
  49.  
  50. // CSVファイルに出力する
  51. sw.WriteLine(GetOutputLineString());
  52. }
  53.  
  54. // Streamを閉じて書き込み
  55. sw.Flush();
  56. sw.Close();
  57.  
  58. // 処理開始のメッセージ
  59. Debug.Log("***** 乱数の偏りの確認を終了 *****");
  60. }
  61.  
  62. void InitializeDiceDicts(){
  63. // それぞれの目が出た回数を保存する辞書を初期化する
  64. diceDict = new Dictionary<int, int>();
  65.  
  66. // それぞれの目が出た確率を保存する辞書を初期化する
  67. probDict = new Dictionary<int, float>();
  68.  
  69. foreach (int pips in dice){
  70. diceDict.Add(pips, 0);
  71. probDict.Add(pips, 0f);
  72. }
  73. }
  74.  
  75. void RollADice(){
  76. // 乱数の範囲指定で配列のインデックスを取得する
  77. int index = Random.Range(0, dice.Length);
  78.  
  79. // サイコロの目を取得する
  80. int pips = dice[index];
  81.  
  82. // 出た目を記録する
  83. diceDict[pips]++;
  84. }
  85.  
  86. void RollDicesWithNumber(int numberOfTimes){
  87. // 指定された回数だけサイコロを振る
  88. for (int i = 0; i < numberOfTimes; i++){
  89. RollADice();
  90. }
  91. }
  92.  
  93. string GetHeaderLineString(){
  94. // StringBuilderを使ってヘッダ行を出力する
  95. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  96.  
  97. // サイコロの目の数だけループ
  98. for (int i = 0; i < dice.Length; i++){
  99. sb.Append(dice[i]);
  100.  
  101. // 配列の最後以外はカンマをつける
  102. if (i != dice.Length - 1){
  103. sb.Append(",");
  104. }
  105. }
  106. return sb.ToString();
  107. }
  108.  
  109. string GetOutputLineString(){
  110. // 辞書のアイテムごとに目が出た確率を出力する
  111. System.Text.StringBuilder sb = new System.Text.StringBuilder();
  112. // サイコロの目の数だけループ
  113. for (int i = 0; i < dice.Length; i++){
  114. int pips = dice[i];
  115. sb.Append(probDict[pips]);
  116.  
  117. // 配列の最後以外はカンマをつける
  118. if (i != dice.Length - 1){
  119. sb.Append(",");
  120. }
  121. }
  122. return sb.ToString();
  123. }
  124.  
  125. void SetProbDictFromDiceDict(){
  126. // それぞれの目が出た回数を確率に直す
  127. for (int i = 0; i < dice.Length; i++){
  128. int pips = dice[i];
  129. float prob = 1.0f * diceDict[pips] / rollNum;
  130. probDict[pips] = prob;
  131. }
  132. }
  133. }
Add Comment
Please, Sign In to add comment