我正在为学校项目做dxball游戏.我正在尝试为将在屏幕上移动的对象(块)创建生成点.但我的问题是当我说创建2个Block类型的对象时.他们将获得相同的随机spawnlocation,他们将始终保持相同的respawn位置.所以我想我正在做的随机功能错误
public class blocks
{
public Texture2D texturBlock1;
public Vector2 hastighet = new Vector2(500.0f, 000.0f);
public Vector2 position;
private Random random = new Random();
private int Screen = new int();
public blocks(int MaxWith)
{
this.position.X = MaxWith+10;
this.position.Y = random.Next(300);
Screen = MaxWith;
}
Run Code Online (Sandbox Code Playgroud)
如果有人可以帮助我,我会很高兴.对于XNA和c#我是一个真正的新手
.NET中的Random类通常使用System.DateTime作为它的种子,所以当你同时创建多个random时,你倾向于获得相同的种子并生成相同的随机#.
尝试使用GUID作为种子的静态方法
public static int RandNumber(int low, int high)
{
Random rndNum = new Random(int.Parse(Guid.NewGuid().ToString().Substring(0, 8), System.Globalization.NumberStyles.HexNumber));
int rnd = rndNum.Next(low, high);
return rnd;
}
Run Code Online (Sandbox Code Playgroud)