可能重复:
随机数生成器仅生成一个随机数
一个初学者的问题。我有一个非常简单的程序来画一条线,我想随机化位置,但是每次我创建一个新的 Random 实例时,它都会返回相同的值。哪里有问题?谢谢你。
private void Draw()
{
Random random1 = new Random();
int randomNumber1 = random1.Next(0, 300);
Random random2 = new Random();
int randomNumber2 = random2.Next(0, 300);
Random random3 = new Random();
int randomNumber3 = random3.Next(0, 300);
Random random4 = new Random();
int randomNumber4 = random4.Next(0, 300);
System.Drawing.Graphics g = this.CreateGraphics();
Pen green = new Pen(Color.Green, 5);
g.DrawLine(green, new Point(randomNumber1, randomNumber2),
new Point(randomNumber3, randomNumber4));
}
private void btndraw1_Click(object sender, EventArgs e)
{
Draw();
}