public static class Utility
{
public static int getRamdom(int _max){
System.Random random = new System.Random((int)DateTime.Now.Ticks);
return random.Next(0, _max);
}
}
void Start()
{
for (int i = 0; i < 10; i++)
{
Debug.Log(Utility.getRamdom(10));
}
}
↑なんか5に滅茶苦茶偏った。
これは偶然なんだろうかと思いもう1回実行すると
↑7と0しか出現しない。
明らかに乱数の戻り値が同じになっているとしか考えられないです。
そこで色々と調べて改良したコード(OKパターン)がこちらです。
public static class Utility
{
private static System.Random random;
public static int getRamdom(int _max){
if (random == null) random = new System.Random((int)DateTime.Now.Ticks);
return random.Next(0, _max);
}
}
public static class Utility
{
public static int getRamdom(int _max){
System.Random random = new System.Random((int)DateTime.Now.Ticks);
return random.Next(0, _max);
}
}
System.Random random = new System.Random(10);
for (int i = 0; i < 5; i++)
{
Debug.Log(random.Next(0, 10));
}
random = new System.Random(10);
for (int j = 0; j < 5; j++)
{
Debug.Log(random.Next(0, 10));
}
コメント