C# - 随机生成器从数组中排除最后一个数字
并且使用Winforms开始了几天的限制。
我刚刚在C#中开始了我的第一个小项目, 从这些随机中选择一个,但不连续两次。
我想我正在寻找类似的东西:
static Random rnd = new Random();
int lastPick;
if (checkBox1.Checked == true)
{
int RandomPick = rnd.Next(pictureBoxArray.Length);
lastPick = RandomPick;
PictureBox picBox = pictureBoxArray[RandomPick **- lastPick**];
picBox.BorderStyle = BorderStyle.FixedSingle;
}
我还尝试创建一个包含我最后一个选择的列表并尝试使用它,但它也没有起作用,它给了我范围的异常。
static Random rnd = new Random();
int lastPick;
List<int> lastNumber = new List<int>();
if (checkBox1.Checked == true)
{
int RandomPick = rnd.Next(pictureBoxArray.Length);
lastPick = RandomPick;
lastNumber.Add(lastPick);
PictureBox picBox = pictureBoxArray[RandomPick - lastNumber.Count];
picBox.BorderStyle = BorderStyle.FixedSingle;
}
任何帮助或迈向正确方向的技巧将不胜感激
I've just started my first little project in C# and with WinForms and I am stuck on this one feature for a few days now..
I have an Array with about 60 PictureBoxes in it and when i press a button, I want it to pick one random out of these, but not twice in a row.
I guess I am searching for something like that:
static Random rnd = new Random();
int lastPick;
if (checkBox1.Checked == true)
{
int RandomPick = rnd.Next(pictureBoxArray.Length);
lastPick = RandomPick;
PictureBox picBox = pictureBoxArray[RandomPick **- lastPick**];
picBox.BorderStyle = BorderStyle.FixedSingle;
}
I've also tried to create a List containing my last Pick and tried to use this, but it also didn't work and it gave me an Out of Range Exception.
static Random rnd = new Random();
int lastPick;
List<int> lastNumber = new List<int>();
if (checkBox1.Checked == true)
{
int RandomPick = rnd.Next(pictureBoxArray.Length);
lastPick = RandomPick;
lastNumber.Add(lastPick);
PictureBox picBox = pictureBoxArray[RandomPick - lastNumber.Count];
picBox.BorderStyle = BorderStyle.FixedSingle;
}
Any help or tips to get into the right direction would be appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我觉得您正在解决一个问题。您可以简单地将最新的索引存储在变量(就像您正在这样做一样)中,然后生成一个随机数,直到它是变量中的一个。这是一个示例代码段:
I feel like you are overcomplicating a problem. You can simply store the latest index in a variable (like you are doing), and then generate a random number until it is something else than the one in the variable. Here's an example code snippet:
您已经接近了,只需随机选择,直到新选秀与上一选菜不同。
You are close, just pick randomly until the new pick is not the same as the previous one.
由于循环时使用了其他答案,因此我想提出一种在没有一段时间循环的情况下进行此操作的方法。创建初始化的索引列表,以将所有可能的索引包含到您的数组中。此解决方案需要
System.linq
。将您以前选择的索引初始化为-1。
在您的数组中创建所有可能的索引的列表。
现在,当您想要索引到数组中时,您可以从索引列表中获取索引。
我们将从索引列表中删除此选择的索引,以便再次选择它。首先,我们需要添加先前删除的索引(如果不是-1),因为它现在是有效的选择。
好处是,如果您想从未显示副本,则可以删除最后选择的索引代码,并且永远不会显示重复。与其他答案相比,这有点长一点,但是想证明还有一种替代与一段时间循环使用蛮力的替代方案。
Since the other answers use while loops, I wanted to present a way to do this without a while loop. Create a list of indices initialized to contain all possible indices into your array. This solution requires
System.Linq
.Initialize your previous chosen index to -1.
Create a list of all possible indices into your array.
Now when you want an index into your array, you get the index from the indices list.
We are going to remove this chosen index from the indices list so it cannot be chosen again. First we need to add back the previously removed index (if it is not -1), since it is now a valid selection.
The nice thing is, if you wanted to never show a duplicate, you can remove the last chosen index code and it will never show a duplicate. This is a bit long winded compared to the other answers, but wanted to show there is an alternative to using brute force with a while loop.