Created
June 18, 2018 13:23
-
-
Save todorok1/860369335453154f97baf26b61b0034c to your computer and use it in GitHub Desktop.
ランダムな値を取得して遊ぶ
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
using System.Collections; | |
using System.Collections.Generic; | |
using UnityEngine; | |
using System.IO; | |
public class RandomTest : MonoBehaviour { | |
// サイコロの目を定義 | |
int[] dice = new int[] {1, 2, 3, 4, 5, 6}; | |
// サイコロの目が出た回数を記録するDictionary | |
Dictionary<int, int> diceDict; | |
Dictionary<int, float> probDict; | |
// 1セットあたりの試行回数 | |
[SerializeField] | |
int rollNum = 10000; | |
// セット数 | |
[SerializeField] | |
int setNum = 1000; | |
void Start(){ | |
OutputDiceRollResults(); | |
} | |
void OutputDiceRollResults(){ | |
// 処理開始のメッセージ | |
Debug.Log("***** 乱数の偏りの確認を開始 *****"); | |
// CSVファイルのヘッダを出力 | |
StreamWriter sw; | |
FileInfo fi; | |
string fineName = Application.dataPath + "/Resources/Csv/dice_output.csv"; | |
fi = new FileInfo(fineName); | |
sw = fi.CreateText(); | |
sw.WriteLine(GetHeaderLineString()); | |
for (int i = 0; i < setNum; i++){ | |
// 辞書を初期化する | |
InitializeDiceDicts(); | |
// サイコロを振る | |
RollDicesWithNumber(rollNum); | |
// それぞれの目が出た回数を確率に直す | |
SetProbDictFromDiceDict(); | |
// CSVファイルに出力する | |
sw.WriteLine(GetOutputLineString()); | |
} | |
// Streamを閉じて書き込み | |
sw.Flush(); | |
sw.Close(); | |
// 処理開始のメッセージ | |
Debug.Log("***** 乱数の偏りの確認を終了 *****"); | |
} | |
void InitializeDiceDicts(){ | |
// それぞれの目が出た回数を保存する辞書を初期化する | |
diceDict = new Dictionary<int, int>(); | |
// それぞれの目が出た確率を保存する辞書を初期化する | |
probDict = new Dictionary<int, float>(); | |
foreach (int pips in dice){ | |
diceDict.Add(pips, 0); | |
probDict.Add(pips, 0f); | |
} | |
} | |
void RollADice(){ | |
// 乱数の範囲指定で配列のインデックスを取得する | |
int index = Random.Range(0, dice.Length); | |
// サイコロの目を取得する | |
int pips = dice[index]; | |
// 出た目を記録する | |
diceDict[pips]++; | |
} | |
void RollDicesWithNumber(int numberOfTimes){ | |
// 指定された回数だけサイコロを振る | |
for (int i = 0; i < numberOfTimes; i++){ | |
RollADice(); | |
} | |
} | |
string GetHeaderLineString(){ | |
// StringBuilderを使ってヘッダ行を出力する | |
System.Text.StringBuilder sb = new System.Text.StringBuilder(); | |
// サイコロの目の数だけループ | |
for (int i = 0; i < dice.Length; i++){ | |
sb.Append(dice[i]); | |
// 配列の最後以外はカンマをつける | |
if (i != dice.Length - 1){ | |
sb.Append(","); | |
} | |
} | |
return sb.ToString(); | |
} | |
string GetOutputLineString(){ | |
// 辞書のアイテムごとに目が出た確率を出力する | |
System.Text.StringBuilder sb = new System.Text.StringBuilder(); | |
// サイコロの目の数だけループ | |
for (int i = 0; i < dice.Length; i++){ | |
int pips = dice[i]; | |
sb.Append(probDict[pips]); | |
// 配列の最後以外はカンマをつける | |
if (i != dice.Length - 1){ | |
sb.Append(","); | |
} | |
} | |
return sb.ToString(); | |
} | |
void SetProbDictFromDiceDict(){ | |
// それぞれの目が出た回数を確率に直す | |
for (int i = 0; i < dice.Length; i++){ | |
int pips = dice[i]; | |
float prob = 1.0f * diceDict[pips] / rollNum; | |
probDict[pips] = prob; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment