use*_*063 5 c# random performance
我正在开发第一个C#头实验室.我在程序中遇到了一个问题,那就是狗的速度相同.我无法弄清楚他们为什么会这样做,因为在我看来,每个对象实例都会在其X位置添加一个随机的1到5个像素.这种随机性应该足以产生差异.
所以,因为我不想发布我的整套实验室1类,我重新创建了一个小型独立版本,只有两只狗赛车,没有下注方面.
Form1.Designer包含: - 两个带有猎犬的图片框.-a开始按钮
Greyhound.cs类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
namespace test
{
public class Greyhound
{
public int DogID;
public PictureBox myPictureBox;
public Point StartingPosition;
public Point CurrentPosition;
public Random Randomizer;
public bool Run()
{
int AddDistance = Randomizer.Next(1, 7);
CurrentPosition.X += AddDistance;
myPictureBox.Location = CurrentPosition;
if (CurrentPosition.X > 600)
{
return true;
}
else
{
return false;
}
}
public void ReturnToStart()
{
CurrentPosition = StartingPosition;
myPictureBox.Location = StartingPosition;
}
}
}
Run Code Online (Sandbox Code Playgroud)
Form1.cs类:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
Greyhound One = new Greyhound() { DogID = 1, myPictureBox = pictureBox1, StartingPosition = pictureBox1.Location, CurrentPosition = pictureBox1.Location, Randomizer = new Random() };
Greyhound Two = new Greyhound() { DogID = 2, myPictureBox = pictureBox2, StartingPosition = pictureBox1.Location, CurrentPosition = pictureBox2.Location, Randomizer = new Random() };
if (One.Run())
{
timer1.Enabled = false;
MessageBox.Show("Dog One WON!");
}
else if (Two.Run())
{
timer1.Enabled = false;
MessageBox.Show("Dog Two WON!");
}
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以看到这个创建的方式有问题吗?为什么狗会以相同的速度运行而不是每次随机1到5个像素的原因.
不要将新的 Random 传递给每只狗,而是这样做:
Random rnd = new Random();
Greyhound One = new Greyhound() { DogID = 1, myPictureBox = pictureBox1, StartingPosition = pictureBox1.Location, CurrentPosition = pictureBox1.Location, Randomizer = rnd };
Greyhound Two = new Greyhound() { DogID = 2, myPictureBox = pictureBox2, StartingPosition = pictureBox1.Location, CurrentPosition = pictureBox2.Location, Randomizer = rnd };
Run Code Online (Sandbox Code Playgroud)
解释
发生这种情况是因为随机对象实际上是半随机的,它们生成使用给定种子数计算的随机数序列。如果您使用 Random() 构造函数,则该种子会在运行时自动生成。但是,如果您按顺序创建两个随机数,就像您在代码中所做的那样,生成的种子将相等,两个随机发生器生成的数字也将相等。
您可以尝试使用更简单的示例:
//Your two dogs have two new Random instantiated in sequence.
//The two random numbers will be always the same
bool areTheSame = new Random().Next() == new Random().Next();
Console.WriteLine("two dogs with two Random are the same? "+ areTheSame);
Random rnd = new Random();
//The two dogs have now the same random object.
//The two random numbers will be almost always different. Almost because the numbers are random and two or more equals number can occur in sequence.
areTheSame = rnd.Next() == rnd.Next();
Console.WriteLine("two dogs using the same random are the same? ~" + areTheSame);
Console.ReadLine();
Run Code Online (Sandbox Code Playgroud)