我正在迈向第一个实验室#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;
}
}
}
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;
}
}
}
任何人都可以看到创建它的方式有问题吗?为什么狗会以相同的速度运行而不是每次随机1到5个像素的原因。
答案 0 :(得分:2)
而不是传递给每只狗一个新的随机做这个:
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 };
<强>解释强>
之所以发生这种情况,是因为随机对象实际上是半随机的,它们会生成一系列随机数,这些随机数是通过使用给定的种子数计算的。
如果使用Random()构造函数,则会在运行时自动生成此种子。但是如果按顺序创建两个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();
答案 1 :(得分:2)
问题在于Random类。当您创建没有参数的新对象时,它使用system clock:
默认情况下,Random类的无参数构造函数使用 系统时钟生成其种子值,同时进行参数化 构造函数可以根据中的刻度数取一个Int32值 当前时间。但是,因为时钟具有有限的分辨率, 使用无参数构造函数创建不同的Random对象 紧密连续创建随机数生成器 相同的随机数序列。
您应该只使用一个Random
类,并插入不同的生成值作为起始速度。