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)
{
List<string> firstnames = new List<string>();
StreamReader reader = new StreamReader("Random First Names.txt");
while (!reader.EndOfStream)
firstnames.Add(reader.ReadLine());
reader.Close();
List<string> lastnames = new List<string>();
reader = new StreamReader("Random Last Names.txt");
while (!reader.EndOfStream)
lastnames.Add(reader.ReadLine());
reader.Close();
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();
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();
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);
}
}
private int RandomGauss(int min, int max, double mean, double sigma){...}
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);
}
}
PopulateAtRandom(1000); . buttonSubmit_Click(), listBox . , : ) 500- - , - , ) . , , , students , . , , listBox.Items, , , . , , . , 20 , , . , , 20 , , , 979 , . , .
, , , , , , . , , , , - , , . , . PopulateAtRandom() , students, , , Student, students , . #, , , . , , , , . !