-1

I wrote some code that generate a random point and also makes random rectangles. All debug seems ok but the code just draw 1 rectangle.

See my code and tell me whats wrong.

private void btnRun_Click(object sender, EventArgs e)
{
    Graphics g = pnlWarZone.CreateGraphics();
    if (int.Parse(txtGenerationCount.Text) > 0)
    {
        RectangleF[] rects = new RectangleF[int.Parse(txtGenerationCount.Text)];

        for (int i = 0; i < int.Parse(txtGenerationCount.Text); i++)
        {
            rects[i] = new RectangleF(GeneratePoint(),new SizeF(4,4));
        }

        g.FillRectangles(new SolidBrush(Color.Blue), rects);
    }
}

UPDATE : This is method for generate point

private Point GeneratePoint()
{
    Random r = new Random();
    //return random.NextDouble() * (maxValue - minValue) + minValue;
    var x =r.Next(_rectangles[0].X, _rectangles[0].Width);
    var y =r.Next(_rectangles[0].Y, _rectangles[0].Height);
    return new Point(x,y);
}
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Davis Miller
  • 59
  • 1
  • 1
  • 12
  • 2
    Are you sure `GeneratePoint()` is creating unique points? – MikeH Dec 19 '18 at 19:54
  • 1
    What does the `GeneratePoint` function look like? If it's creating a new instance of Random than it's likely getting the same seed each time and you're drawing each rectangle on top of the last. – Jonathon Chase Dec 19 '18 at 19:54
  • 1
    _pnlWarZone.CreateGraphics();_ Usually a bad idea. – TaW Dec 19 '18 at 19:57
  • 3
    STEP THROUGH THE DEBUGGER AND *LOOK* AT THE VALUE OF EACH NEW RECTANGLE. Once you know what's actually going into `rects[i]`, you can work backwards to determine why you're not getting the results you expected. PS: Use `this.Invalidate(); // force Redraw the form`, and do your graphics in the paint callback. – paulsm4 Dec 19 '18 at 19:58
  • Is this WinForms? That's not how you draw to the screen; store `new RectangleF`, call `pnlWarZone.Invalidate()`, and draw the rectangles in your `Paint` handler. – Dour High Arch Dec 19 '18 at 20:05
  • 3
    I'd bet a nickel that `GeneratePoint` is creating a `new Random()` each time it is called. – D Stanley Dec 19 '18 at 20:35
  • 3
    @DStanley With this level of communication we'll never know. – MikeH Dec 19 '18 at 20:41
  • Sorry for delay on response , i update topic and put generate point code. – Davis Miller Dec 21 '18 at 12:20
  • I need to generate point and add point in rectangle that later i draw. – Davis Miller Dec 21 '18 at 12:24
  • @DStanley was right. – Uwe Keim Dec 21 '18 at 12:45
  • @UweKeim i found problem ! my method generate duplicate number. – Davis Miller Dec 21 '18 at 12:49
  • After your edit, looks like a duplicate of [C# Random Numbers aren't being “random”](https://stackoverflow.com/q/7251714) and [Random encounter not so random](https://stackoverflow.com/q/2727538). – dbc Dec 21 '18 at 21:08

1 Answers1

0

Your code most likely looks something like this:

private Point GeneratePoint() {
  Random rnd = new Random();
  int x = rnd.Next(0, pnlWarZone.ClientSize.Width);
  int y = rnd.Next(0, pnlWarZone.ClientSize.Height);
  return new Point(x, y);
}

What you want to do is only generate a new Random object once, and always re-use that variable later:

Random rnd = new Random();

private Point GeneratePoint() {      
  int x = rnd.Next(0, pnlWarZone.ClientSize.Width);
  int y = rnd.Next(0, pnlWarZone.ClientSize.Height);
  return new Point(x, y);
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225