c# - 列表在不应该使用重复数据时填充

标签 c# list memory 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 */
        }
    }

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

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

最佳答案

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 ...
}

你想要更像这样的东西:

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 ...
}

附言——有一些实用方法可以帮助从文件中读取文本。

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

关于c# - 列表在不应该使用重复数据时填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47479917/

相关文章:

c# - FreeSWITCH : Is there a way to get the audio stream from other leg via the native API

python - 从多个列表中创建一个列表

r - 更改列表中数据框中的列

node.js - 为什么process.memoryUsage()不输出 Node 进程消耗的内存

c++ - 如何解释内存分配延迟的急剧跳跃?

c# - Azure - WebAPI 无法连接到 Azure DB,错误 : The system cannot find the file specified [SqlException]

c# - 检查 RoutedEvent 是否有任何处理程序

c# - 从 XML 生成类 : xsd. 找不到 exe

python - 在 Python 中根据索引及其后面的项目对列表进行切片

C++ 对象堆分配和成员变量