0

I am trying to display several points with x and y coordinates between 0 and 200 in a chart control on a Windows form app in C#. My code looks like the following:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public class Point
        {
            public double X;
            public double Y;

            public Point(double x, double y)
            {
                X = x;
                Y = y;
            }
        }

        public Form1()
        {

            InitializeComponent();

            List<Point> points = new List<Point>();

            for (int i=0; i<5; i++)
            {
                points.Add(new Point(GetRandomNumber(0, 200), GetRandomNumber(0, 200)));
            }

            foreach(Point f in points)
            {
                chart1.Series["Series1"].Points.AddXY(f.X, f.Y);
            }

            Application.DoEvents();

        }

        double GetRandomNumber(double minimum, double maximum)
        {
            Random random = new Random();
            return random.NextDouble() * (maximum - minimum) + minimum;
        }
    }
}

When I run this, however, I get this plot:

enter image description here

I get a similar result no matter what range I use. For example, the following output is for x and y in the range of 0, 30:

enter image description here

However, when I manually enter some random points to the list, the chart scales appropriately, and they all show up o it just fine:

List<Point> points = new List<Point>
            {
                new Point(10, 29),
                new Point(5, 16),
                new Point(27, 8),
                new Point(17, 23),
                new Point(22, 13)
            };

enter image description here

Why is this? and how can I get all points to display appropriately, when randomly generated.

I am using:

Microsoft Visual Studio Community 2017 
Visual C# 2017   00369-60000-00001-AA613
Microsoft Visual C# 2017
Adi
  • 75
  • 1
  • 9
  • Why you can not use System.Drawing.Point? Can you look at count of generated points? Entert Console.WriteLine(points.Count); After you are adding points to it. And check values of generated points – Sergey Anisimov Dec 17 '17 at 14:14
  • @Pollyflow, I can't use system.Drawing.Point because it only allows integer values. I displayed the number of points in the list using a label on the form, and it displays the correct number of points. – Adi Dec 17 '17 at 14:28
  • System.Drawing.PointF is for float values. What about point values? – Sergey Anisimov Dec 17 '17 at 14:33
  • @Pollyflow, Printing out the point values, looks like the random number generator generated the same point every time. Any idea why that would happen? – Adi Dec 17 '17 at 16:02
  • 1
    It is a traditional bug, the code is creating a new Random instance for every single point. And therefore generates the exact same random number repeatedly since the seed is the same. They are all on top of each other in the graph. Make it a field of the class instead so it is initialized only once. – Hans Passant Dec 17 '17 at 16:04
  • @HansPassant, That makes sense... Everything works now! Thanks! – Adi Dec 17 '17 at 16:21

1 Answers1

2

Answering My own question with the help of @HansPassant. Apparently, because I was creating a new random object every time, the random number generator was generating the same number every time, and all the points were on top of each other. I fixed it by declaring a 'random' object in the constructor and then passing it into my 'GetRandomNumber' function.

Adi
  • 75
  • 1
  • 9