3
可能重复:
Random number generator not working the way I had planned (C#)Random.Next()始终给予相同的结果
我经历了基本的C#编程,我目前做一个滚模。这个应用程序可以卷起四个骰子。我遇到的问题是骰子总是产生相同的结果。我使用了一种方法,其中随机数生成器从1到6生成一个数字,然后选择适当的图片。我为每个图像框重复下面的方法,因为用户可以输入他们想要滚动的骰子数量。我的问题是,骰子每次都会产生相同的图片。我究竟做错了什么?
public Image displaypic(PictureBox box)
{
string picchoice;
int number;
Image picture = box.Image;
//Prevents Redundant Images
Image displaying = box.Image;
do
{
//picks a die to display
Random rand = new Random();
number = rand.Next(1, 7);
picchoice = number.ToString();
//select an image from the image selection method
picture = diepic(picture, picchoice);
}
while (picture == displaying);
//return image
return picture;
}