CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2013
    Posts
    16

    Logic error in my program , object print the same values..

    this is my Program Class:
    Code:
        class Program
        {
            static void Main(string[] args)
            {
                Rectangle[] field = new Rectangle[2];
                for (int i = 0; i < field.Length; i++)
                {
                    field[i] = new Rectangle();
                    field[i].SetRectangleRandomly();
                }
                for (int i = 0; i < field.Length; i++)
                {
    
                    Console.WriteLine(" Point = ({0},{1}) , Height = {2} , Width = {3}", field[i].TopLeft.X, field[i].TopLeft.Y, field[i].Height, field[i].Width);
                }
             
                Console.ReadLine();
    
            }
        }
    and this is my Rectangle class:

    Code:
      class Rectangle
        {
            private Point topLeft;
            private int height;
            private int width;
            private Random rnd = new Random();
    
    
            public Point TopLeft
            {
                get { return topLeft; }
                set { topLeft = value; }
            }
    
            public int Height
            {
                get { return height; }
                set { height = value; }
            }
    
    
            public int Width
            {
                get { return width; }
                set { width = value; }
            }
    
         // הצבת ערכים בצורה דינמית למלבן
            public void SetRectangleRandomly()
            {
               
                topLeft = new Point();
                this.topLeft.X = rnd.Next(0, 1000);
                this.topLeft.Y = rnd.Next(0, 1000);
                this.Height =rnd.Next(0, 1000);
                this.Width = rnd.Next(0, 1000);
            }
    
    
    
    
    
    
        }
    i do not know why it print the same values
    becouse i inited the rectangle array in the main..

  2. #2
    Join Date
    Feb 2004
    Location
    Seattle, USA
    Posts
    137

    Re: Logic error in my program , object print the same values..

    It is because you are creating a new Random object for each instance. If you change it to the following, then it will create different objects.
    Code:
    private static Random rnd = new Random()
    The reason for this is that Random uses the internal clock as its initial seed and updates the seed after each call. When you create many Random objects quickly, then they all start out with the same initial seed & produce the same random values.
    Last edited by enfekted; September 30th, 2013 at 09:03 PM. Reason: adding more details

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured