gpt4 book ai didi

c# - Random() 将相同的值分配给相同类型的新对象

转载 作者:太空狗 更新时间:2023-10-30 01:00:01 25 4
gpt4 key购买 nike

很抱歉我的标题不太明确,我发现很难深入描述这个问题。我试图通过 SO 研究与 Random 类似的问题,并实现了这些建议,但是没有成功。

我有以下模拟体育比赛的代码。逻辑的核心在于Game类。当我模拟锦标赛时,第一轮比赛中分配给玩家的结果很好,如预期的那样随机分配。然而,当我模拟第二轮和第三轮等等时,第一轮获胜的玩家获得的分数与第一轮完全相同。就好像系统记住了它们,而不是生成新的随机值。我确定它与 Random 有关,但我无法理解它。有人对我如何解决这个问题有任何建议吗?

RnDGen:

public class RndGen
{
private static Random random = new Random();

public static int Get(int min, int max)
{
return random.Next(min, max);
}
}

锦标赛:

public class Tournament : IAggregateRoot
{
public string Name { get; set; }

public int Year { get; set; }

public List<Round> Rounds { get; set; }

public GameMaster GameMaster { get; set; }

public Tournament(string name, int year)
{
Rounds = new List<Round>();
Name = name;
Year = year;
}

public void Simulate(List<Player> players)
{
var playersStillInTournament = players;
int roundNumber = 1;

while (playersStillInTournament.Count > 1)
{
var round = new Round(roundNumber);

round.Simulate(playersStillInTournament);
playersStillInTournament = round.Winners;

Rounds.Add(round);

roundNumber++;
}
}
}

回合:

public class Round
{
public int Number { get; private set; }

public List<Game> Games { get; set; }

public List<Player> Winners => Games.Select(g => g.Winner).ToList();

public Round(int number)
{
Number = number;
Games = new List<Game>();
}

public void Simulate(List<Player> players)
{
for(int i = 0; i != players.Count; i = i + 2)
{
var playersToAdd = players.Skip(i).Take(2).ToList();
var game = new Game(playersToAdd);
Games.Add(game);
}
}
}

游戏:

public class Game
{
public List<Player> Players { get; private set; }

private int MaxSets => (Players.First().Gender == Gender.Male) ? 5 : 3;

public Player Winner => Players.Find(p => p.SetPoints.Count(px => px == 6) == ((MaxSets + 2 - 1) / 2));

public Game(List<Player> players)
{
Players = players;
Simulate();
}

private void Simulate()
{
while (Winner == null)
{
var winnerId = Players[RndGen.Get(0, Players.Count)].Id;
var loserPoints = RndGen.Get(0, 5);

Players.Find(p => p.Id == winnerId).SetPoints.Add(6);
Players.Find(p => p.Id != winnerId).SetPoints.Add(loserPoints);
}
}
}

播放器

public class Player : Person
{
public List<int> SetPoints;

public int Points
{
get
{
return SetPoints.Sum();
}
}

public Player(string[] textFileLines, Gender gender)
: base(textFileLines, gender)
{
SetPoints = new List<int>();
}

人物

public abstract class Person
{
public int Id { get; private set; }

public string FirstName { get; private set; }

public string MiddleName { get; private set; }

public string LastName { get; private set; }

public DateTime Birthday { get; private set; }

public string Country { get; private set; }

public string CountryCode { get; private set; }

public Gender Gender { get; set; }

// Returns the age as an integer
public int GetAge => DateTime.Now.Year - Birthday.Year;

protected Person(string[] textFileLines, Gender gender)
{
Id = Convert.ToInt32(textFileLines[0]);
FirstName = textFileLines[1];
MiddleName = textFileLines[2];
LastName = textFileLines[3];
Birthday = Convert.ToDateTime(textFileLines[4]);
Country = textFileLines[5];
CountryCode = textFileLines[6];
Gender = gender;
}

}

最佳答案

将您的 Simulate() 更改为

private void Simulate()
{
while (Winner == null)
{
var winnerId = Players[RndGen.Get(0, Players.Count)].Id;
var loserPoints = RndGen.Get(0, 5);

Players.Find(p => p.Id == winnerId).SetPoints.Add(6);
Players.Find(p => p.Id != winnerId).SetPoints.Add(loserPoints);
}

Console.WriteLine(Winner.Id);
Players.ForEach(p => p.SetPoints = new List<int>());
}

以便您在回合结束时重置每个玩家的 SetPoints

运行 5 次模拟后我得到了这个输出

5
2
2
1
1

关于c# - Random() 将相同的值分配给相同类型的新对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50165720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com