Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7165429
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T14:14:16+00:00 2026-05-28T14:14:16+00:00

I have two classes, a Teacher class and a Student class. In my program

  • 0

I have two classes, a Teacher class and a Student class. In my program I create an array of 10 Teacher objects and within each Teacher object is an array of 10 Student Objects. Each Student object also has an array of integers as a member variabe, and when each Student is instantiated, it’s own array of integers are filled with numbers from a random number generator. My program goes something like this:

  • An array of type Teacher is created with size 10
  • The array is then filled with 10 actual teacher objects
  • Each Teacher object contains an array of Student objects of size 10 as a member variable
  • The Student array in each Teacher object is filled with actual Student objects
  • Each student object has an array of integers that are filled with random numbers in the Student objects constructor.

Here is the problem I ran into: It seems that every time the 10 Student objects are created for each Teacher, the random number generator within the Student object constructor does not reset or change even when I call the .Next() function until the next set of 10 Student objects are created for the next Teacher object. What I want is the 10 Teacher objects to each have their own Student Objects which have their own integer arrays filled with randomly generated numbers.

Any help would be appreciated! I ran into this sort of problem with constructors with my last question, and that was a matter of whether something was to be static or not and I’m not sure that’s the case this time. Please ask me questions if I wasn’t clear enough about anything!!

UPDATE** So after looking at MSDN, I found in their sample code “Thread.Sleep(2000)” and stuck it into my Student Constructor just to see what it did. It seemed to have solved the problem although my program runs a lot slower now, is there a minimum sleep value to wait until the Random.Next() uses a new seed from the clock and even if I did solve the problem, is there a better way to do this? My random number generator is already a static member variable of Student.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T14:14:17+00:00Added an answer on May 28, 2026 at 2:14 pm

    If you initialize several Random instances using the default constructor, Random(), within a short time period, then you risk that they would end up with the same seed value (which is time-dependant) and, therefore, generate the same random sequence each time.

    You can fix this issue by initializing a single static Random instance, and share it among all Student instances. This is perfectly safe as long as you’re not multi-threading.

    public class Student
    {
        private static readonly Random random = new Random();        
    }
    

    From the MSDN page on the Random() constructor:

    The default seed value is derived from the system clock and has finite resolution. As a result, different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers. This problem can be avoided by using a single Random object to generate all random numbers. You can also work around it by modifying the seed value returned by the system clock and then explicitly providing this new seed value to the Random(Int32) constructor. For more information, see the Random(Int32) constructor.

    Edit: Although you state that your random number generator is already a static member, this is still not enough if you’re instantiating it repeatedly (redundantly) during each Student initialization. The following code is still incorrect:

    public class Student
    {
        private static Random random;
    
        private int[] numbers;
    
        public Student()
        {
            random = new Random();
            numbers = new int[10];
            for (int i = 0; i < 10; ++i)
                numbers[i] = random.Next();
        }
    }
    

    You need to replace it with a version that only instantiates Random once, like so:

    public class Student
    {
        private static readonly Random random = new Random();
    
        private int[] numbers;
    
        public Student()
        {
            numbers = new int[10];
            for (int i = 0; i < 10; ++i)
                numbers[i] = random.Next();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes, Student and Teacher, Student has one concrete method: takeCourse; Teacher
I have two classes defined in different h files and each class has some
I have two classes that extend the activity class. Each class has it's own
I have two classes Teacher and Student . I am trying to get teacher
I have two classes Event and Review. The event has an instance object Review
I have two classes with a parent-child relationship (the Parent class has-a Child class),
I have two classes, Foo and Bar. Each Foo has a name and a
I have two classes: class Object { public: Object(); virtual void update(); virtual void
I have two classes, and want to include a static instance of one class
I have two classes, Foo and Bar, that have constructors like this: class Foo

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.