Created
June 24, 2018 07:56
-
-
Save todorok1/056bcdd650cc454666a40b56f905fa07 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 RandomWithCurve : MonoBehaviour { | |
[SerializeField] | |
AnimationCurve curve; | |
[SerializeField] | |
string outputFileName; | |
int sampleNum = 10000; | |
SortedDictionary<int, int> probDict = new SortedDictionary<int, int>(); | |
void Start(){ | |
OutputRandomResults(); | |
} | |
void OutputRandomResults(){ | |
// 処理開始のメッセージ | |
Debug.Log("***** 乱数からカーブを使って重み付けするテストを開始 *****"); | |
// CSVファイルのヘッダを出力 | |
StreamWriter sw; | |
FileInfo fi; | |
string fineName = Application.dataPath + "/Resources/Csv/RandomCurve/" + outputFileName + ".csv"; | |
fi = new FileInfo(fineName); | |
sw = fi.CreateText(); | |
sw.WriteLine("取得した値,発生回数,発生確率"); | |
for (int i = 1; i <= sampleNum; i++){ | |
float randomValue = Random.value; | |
float weightedValue = curve.Evaluate(randomValue); | |
int roundedValue = Mathf.RoundToInt(weightedValue * 100); | |
if (probDict.ContainsKey(roundedValue)){ | |
probDict[roundedValue]++; | |
} else { | |
probDict.Add(roundedValue, 1); | |
} | |
} | |
foreach (KeyValuePair<int, int> pair in probDict){ | |
System.Text.StringBuilder sb = new System.Text.StringBuilder(); | |
float prob = 1.0f * pair.Value / 100; | |
sb.Append(pair.Key).Append(",").Append(pair.Value).Append(",").Append(prob); | |
sw.WriteLine(sb.ToString()); | |
} | |
// Streamを閉じて書き込み | |
sw.Flush(); | |
sw.Close(); | |
// 処理開始のメッセージ | |
Debug.Log("***** 乱数からカーブを使って重み付けするテストを終了 *****"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment