A list populating duplicate data when it should not

I am working on a programming project for a class, and I wanted to add something extra to the project, randomly generating data for it. My problem is that I have a list populating copies of the same data, although it seems to generate completely different things every time a new object is created. When I try to debug, I come across very strange behavior. This is my code:

    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 . , : ) 500- - , - , ) . , , , students , . , , listBox.Items, , , . , , . , 20 , , . , , 20 , , , 979 , . , .

, , , , , , . , , , , - , , . , . PopulateAtRandom() , students, , , Student, students , . #, , , . , , , , . !

+4
1

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();
+5

Source: https://habr.com/ru/post/1689801/


All Articles