如何得到随机两次
本文关键字:两次 何得 随机 | 更新日期: 2023-09-27 18:11:26
我开始学习c#。我尝试着创造一些奇怪的游戏,但我并没有将随机变成随机,也就是两次随机。我试着随机做了两次,但我做不到。Bug是|x |还是|x |不显示,工作。(c#)我怎么能做到这一点?我想把随机变成随机
static void randomata()
{
Random r = new Random();
Random r2 = new Random();
Random r3 = new Random();
Random r4 = new Random();
int randomsonuc = r.Next(0, 100);
int randomsonuc2 = r2.Next(0, 100);
int randomsonuc3 = r3.Next(0, 100);
int randomsonuc4 = r4.Next(0, 100);
if (randomsonuc < 90) // work
{
if (randomsonuc2 < 50) // work
{
cmake("|XX|", 8, 0);
}
else if(randomsonuc2 >= 50) //work
{
cmake("|X|", 8, 0);
cmake("|X|", 8, 1);
}
}
else if (randomsonuc >= 90 && randomsonuc < 95) // work
{
if (randomsonuc3 < 50) // doesnt work
{
cmake("|X |", 8, 0);
}
if (randomsonuc3 >= 50) // work
{
cmake("|X|", 8, 0);
cmake("| |", 8, 1);
}
}
else //(randomsonuc >= 95 && randomsonuc < 100) // work
{
if (randomsonuc4 < 50) // doesnt work
{
cmake("| X|", 8, 0);
}
if (randomsonuc4 >= 50) // work
{
cmake("| |", 8, 0);
cmake("|X|", 8, 1);
}
}
}
因为System.Random()是以当前时间为种子的,所以创建大量这样的实例不会有太大作用
尝试创建一个Random的实例,然后在任何需要随机数的地方使用该实例
static void randomata()
{
Random r = new Random();
int randomsonuc = r.Next(0, 100);
int randomsonuc2 = r.Next(0, 100);
int randomsonuc3 = r.Next(0, 100);
int randomsonuc4 = r.Next(0, 100);
...
}