Your random students aren't random!

Mar 16, 2008 at 11:17 AM
Using Random's default constructor, the random seed is based on system time. The system clock has finite resolution, so successive calls will produce the same results if you use a new Random class every time. Generating 1 million random students in a loop will actually result in a non-random collection of students - my computer will generate clumps of up to 300 students each having the same 'random' numerical first name. Behaviour will be dictated by the resolution of your system time, and the speed of your processor.

For a demonstration, try this:
int[] rands = new int10000;
for (int i = 0; i < rands.Length; i++)
randsi = new Random().Next();

for (int i=0; i<rands.Length; i++)
Console.WriteLine("{0}: {1}", i, randsi);


//Needs to be declared here, not in the method body
static Random randomClass = new Random();

public static Student CreateRandomStudent(int nameRangeStart, int nameRangeEnd)
{
string[] lastNames = Hallstrom","Erickson","Jones","Clinton","Bush;

Student stud = new Student();
int potentialFirstNameIndex = randomClass.Next(nameRangeStart, nameRangeEnd);
stud.FirstName = new StudentName(potentialFirstNameIndex.ToString());
stud.LastName = lastNamesrandomClass.Next(0, lastNames.Length - 1);
stud.GradePointAverage = ((double)randomClass.Next(0, 40)) / 10.0;
return stud;
}
Coordinator
Mar 23, 2008 at 4:26 PM
Thanks for the update - I have a series of updates to do in order to get some things in, and this is included.