列表填充时不应重复数据

Kod*_*bla 4 c# memory list repeat

我正在为类编程项目,我想通过随机生成数据为项目添加额外的东西.我的问题是我有一个列表,其中填充了相同数据的副本,即使每次创建新对象时它似乎都会产生完全不同的东西.当我尝试调试时,我遇到了非常奇怪的行为.这是我的代码:

    private void PopulateAtRandom(int amount)
    {
        // create a list of first names from a text file of 200 names
        List<string> firstnames = new List<string>();
        StreamReader reader = new StreamReader("Random First Names.txt");
        while (!reader.EndOfStream)
            firstnames.Add(reader.ReadLine());
        reader.Close();

        // create a list of last names from a text file of 500 names
        List<string> lastnames = new List<string>();
        reader = new StreamReader("Random Last Names.txt");
        while (!reader.EndOfStream)
            lastnames.Add(reader.ReadLine());
        reader.Close();

        // create a list of majors from a text file of 198 majors
        List<string> majors = new List<string>();
        reader = new StreamReader("Majors.txt");
        while (!reader.EndOfStream)
        {
            string line = reader.ReadLine();
            majors.Add(line.Substring(0, line.IndexOf(" - ")));
        }
        reader.Close();

        // create a list of high schools from a text file of 860 schools
        List<string> highschools = new List<string>();
        reader = new StreamReader("All Illinois High Schools.txt");
        while (!reader.EndOfStream)
            highschools.Add(reader.ReadLine().Split(',')[0]);
        reader.Close();

        // create a list of colleges from a text file of 9436 schools
        List<string> colleges = new List<string>();
        reader = new StreamReader("All US Colleges.txt");
        while (!reader.EndOfStream)
            colleges.Add(reader.ReadLine());
        reader.Close();

        students = new List<Student>();
        for (int i = 0; i < amount; i++)
        {
            bool graduate = random.NextDouble() >= 0.5;
            string fName = firstnames[random.Next(firstnames.Count)];
            string lName = lastnames[random.Next(lastnames.Count)];
            string major = majors[random.Next(majors.Count)];
            int gradYear = RandomGauss(1950, 2017, 2013, (graduate ? 10 : 4));
            string prevSchool = graduate ? colleges[random.Next(colleges.Count)] 
                : highschools[random.Next(highschools.Count)];
            string otherInfo = graduate ? RandomWithDefault<string>(major, 0.05, majors)
                : "" + RandomGauss(0, 60, 0, 15) + " transfer credits";

            Student student = new Student(graduate, fName, lName, major, gradYear, prevSchool, otherInfo);
            students.Add(student); /* I put a breakpoint here for debugging */
        }
    }

    /**
     * <summary>
     * Return a random integer in the given range based on the specified gaussian distribution
     * </summary>
     */
    private int RandomGauss(int min, int max, double mean, double sigma){...}

    /**
     * <summary>
     * Randomly return either the default value or a different value based on the given odds
     * </summary>
     */
    private T RandomWithDefault<T>(T defaultValue, double oddsOfOther, List<T> otherOptions){...}

    private void buttonSubmit_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < students.Count; i++)
        {
            Student student = students[i];
            listBox.Items.Add(student); /* I put another breakpoint here for debugging */
        }
    }
Run Code Online (Sandbox Code Playgroud)

我一直PopulateAtRandom(1000);在我的构造函数中使用.当buttonSubmit_Click()被调用时,listBox将显示两件事之一.第一个条目始终是唯一的,然后a)下一个500-ish条目是一个学生,其余的是第二个学生,或b)其余条目在两个不同的学生之间交替.但是,当我去调试时,我可以看到每个新的条目students都是唯一的,因为它应该是.然后,当我检查如何listBox.Items填充时,我发现前几个的相同模式是唯一的,其余模式只显示两个不同的学生.调试的实际行为似乎也会影响到这一点.例如,我将在第一个断点处停留20次,然后让程序自行完成,直到我到达第二个断点.当我停在第二个断点时,我发现这20个学生中的每一个,再加上另一个正确显示,然后以下979遵循与之前相同的模式.无论我在第一个断点停留多少次,我都会看到同样的效果.

我曾尝试在互联网上搜索此行为的类似实例,但我没有得到任何地方,这可能是因为我不确定如何说出这个问题.当我使用我为此问题提供的标题进行搜索时,我没有得到与我的问题有关的任何内容,所以如果你们中的任何人都知道类似的问题,请指出我正确的方向.我唯一想到的是这是内存分配的问题.PopulateAtRandom()在尝试填充之前students,该方法使用了我创建的列表的大量内存,因此可能程序为每个新的回收相同的内存地址Student,并且由于students实际上只是一个内存地址列表,它最终会重复相同的地址.C#似乎没有很好的方式给我一个对象的内存地址,所以我无法确认.如果是这种情况,我仍然不确定如何规避这个问题,所以任何建议都会非常感激.谢谢!

Str*_*ior 5

RandomGauss可能利用Random类,它根据实例化的时间创建种子.我猜这个RandomGauss方法Random每次调用时都会实例化一个新实例.当你没有调试时,你的循环会在系统的时钟滴答变换时间之前重复很多次,所以很多Random实例最终会使用相同的种子,因此当你第一次询问它们时会产生相同的结果数.

解决方案是创建单个Random实例并将其存储到类的字段中.

例如,而不是这个:

/**
 * <summary>
 * Return a random integer in the given range based on the specified gaussian distribution
 * </summary>
 */
private int RandomGauss(int min, int max, double mean, double sigma){
    Random random = new Random();
    // code that uses random ...
}
Run Code Online (Sandbox Code Playgroud)

你想要更像这样的东西:

private Random random = new Random();

/**
 * <summary>
 * Return a random integer in the given range based on the specified gaussian distribution
 * </summary>
 */
private int RandomGauss(int min, int max, double mean, double sigma){
    // code that uses random ...
}
Run Code Online (Sandbox Code Playgroud)

PS - 有一些实用程序方法可以帮助您从文件中读取文本.

    // create a list of first names from a text file of 200 names
    List<string> firstnames = File.ReadAllLines("Random First Names.txt").ToList();
Run Code Online (Sandbox Code Playgroud)

  • 接得好!它是一个类字段,但我在`RandomGauss()`方法中创建了一个新实例.我删除了,程序运行得很漂亮.谢谢! (2认同)