mardi 21 avril 2015

random seeding issue and probably constructor issue

I want to generate a userID starting with 1000, incrementing by one for each user (1000, 1001, 1002, etc.). Neither location to seed the random statement seems to seed it... (either in the constructor or main). Why does my random statement not initialize with the 1000 seed properly in the following code?

public class Student
{
    public string FullName { get; set; }
    public int StudentID { get; set; }

    //constructor to initialize FullName and StudentID
    public Student(string name, int ID)
    {
        FullName = name;
        Random rnd = new Random();
        StudentID = rnd.Next(1000, 1050); // creates a number greater than 1000
        return;
    }
    public override string ToString()
    {
        return string.Format("ID: {0}\n Name: {1}", StudentID, FullName);
    }
}

public class StudentTest
{
    static void Main(string[] args)
    {
        Student student1 = new Student("Amy Lee", 1000);
        Student student2 = new Student("John Williams", 1001);
        Console.WriteLine(student1);
        Console.WriteLine(student2);
        Console.WriteLine("\nPress any key to exit program");
        Console.ReadKey();
    }
}




Aucun commentaire:

Enregistrer un commentaire