编程语言
首页 > 编程语言> > c# – 列出不应重复填充数据的列表

c# – 列出不应重复填充数据的列表

作者:互联网

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

    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-ish条目是一个学生,其余的是第二个学生,或b)其余条目在两个不同的学生之间交替.但是,当我去调试时,我可以看到每一个进入学生的新入口都应该是唯一的.然后,当我检查如何填充listBox.Items时,我发现前几个的相同模式是唯一的,其余模式只显示两个不同的学生.调试的实际行为似乎也会影响到这一点.例如,我将在第一个断点处停留20次,然后让程序自行完成,直到我到达第二个断点.当我停在第二个断点时,我发现这20个学生中的每一个,再加上另一个正确显示,然后以下979遵循与之前相同的模式.无论我在第一个断点停留多少次,我都会看到同样的效果.

我曾尝试在互联网上搜索此行为的类似实例,但我没有得到任何地方,这可能是因为我不确定如何说出这个问题.当我使用我为此问题提供的标题进行搜索时,我没有得到与我的问题有关的任何内容,所以如果你们中的任何人都知道类似的问题,请指出我正确的方向.我唯一想到的是这是内存分配的问题. PopulateAtRandom()方法在尝试填充学生之前使用我创建的列表占用了大量内存,因此可能该程序为每个新学生回收相同的内存地址,并且由于学生实际上只是一个内存地址列表,它最终会重复相同的地址. 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 ...
}

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

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

标签:c,memory,list,repeating
来源: https://codeday.me/bug/20190607/1195649.html