why doesn't this simple C# tryout work [duplic

2019-07-16 07:34发布

问题:

This question already has an answer here:

  • Random number generator only generating one random number 9 answers

this creates streaks instead of dots. why ? I am trying to paint individual pixels. another method was also tried (using fillrectangle) , which also didn't give the desired result, got bars instead of dots.

protected override void OnPaint(PaintEventArgs pea )
    {

         Graphics g = pea.Graphics ;

         for ( int y = 1 ; y <= Width ; y++ )
         {
            for (  int x  =  1  ; x <=  Height    ; x++ )
            {
                System.Random rand = new  System.Random() ;
                Color c =   (Color.FromArgb(rand.Next(256),
                                    rand.Next(256),
                                    rand.Next(256)));
            // SolidBrush myBrush = new SolidBrush(c);
            // g.FillRectangle(myBrush, x, y, 1, 1);


                 Bitmap pt = new Bitmap(1, 1);
                 pt.SetPixel(0, 0, c);
                 g.DrawImageUnscaled(pt, x, y);

            }
        }

}

what is happening here ?

回答1:

You should not create a new Random object each time. Since that will give you the exact same "random" numbers repeated over and over.

Instead use the same Random object and just call Next on in.



回答2:

You need to create the System.Random object outside the loops. When initialized inside the loop it keep getting the same seed (since it's initialized according to the system clock, which would still have the same value) and therefore the same color.



回答3:

Yeah, what they said.

and you might get better mileage bitblitting something like that. e.g. draw it offscreen and then paint it all at once.

protected override void OnPaint(PaintEventArgs pea)
{
    using (var b = new Bitmap(this.Width, this.Height))
    {
        using (var g = Graphics.FromImage(b))
        {
            var rand = new Random();

            for (int x = 0; x <= Width; x++)
            {
                for (int y = 0; y <= Height; y++)
                {
                    Color c = (Color.FromArgb(rand.Next(256),
                                              rand.Next(256),
                                              rand.Next(256)));
                    using (var newPen = new Pen(c, 1))
                    {
                        g.DrawRectangle(newPen, x, y, 0.5f, 0.5f);
                    }
                }
            }
        }

        pea.Graphics.DrawImage(b, 0, 0);
    }
}

protected override void OnPaint(PaintEventArgs pea)
{
    Graphics g = pea.Graphics;
    var rand = new Random();

    for (int x = 0; x <= Width; x++)
    {
        for (int y = 0; y <= Height; y++)
        {
            Color c = (Color.FromArgb(rand.Next(256),
                                      rand.Next(256),
                                      rand.Next(256)));
            using (var newPen = new Pen(c, 1))
            {
                g.DrawRectangle(newPen, x, y, 0.5f, 0.5f);
            }
        }
    }
}y


回答4:

You need to move the System.Radom outside of the loop. What is happening here is that you are creating the same random point every time the loop iterates. Also if you add using System; to the start of the program than you can simply say Random (but that is not important just a side note). Once the Random is created you can then get the next number by simply calling Next on the object. Try this out and I believe it will rid you of your problems!



回答5:

You are drawing a different coloured dot on every pixel in the rectangle 0,0,Width,Height. But as the previous answers state because random is being intialised to the system clock within each loop, the loop is drawing the same colour giving you a streak of the same colour (Horizontally?) until the system clock ticks on a tiny bit and gives you a different seed.

If you move the System.Random outside the loop so you get a different colour each Next then you will see dots.