C# - 随机生成器从数组中排除最后一个数字

发布于 2025-01-28 09:00:54 字数 991 浏览 1 评论 0原文

并且使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁梦 2025-02-04 09:00:55

我觉得您正在解决一个问题。您可以简单地将最新的索引存储在变量(就像您正在这样做一样)中,然后生成一个随机数,直到它是变量中的一个。这是一个示例代码段:

int lastPick;
while (true) {
  int randomPick = rnd.Next(length);
  if (randomPick != lastPick) {
    lastPick = randomPick;
    // Do things here.
    break; // This breaks the loop.
  }
  // If the previous if-statement was false, we ended
  // up with the same number, so this loop will run again
  // and try a new number
}

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:

int lastPick;
while (true) {
  int randomPick = rnd.Next(length);
  if (randomPick != lastPick) {
    lastPick = randomPick;
    // Do things here.
    break; // This breaks the loop.
  }
  // If the previous if-statement was false, we ended
  // up with the same number, so this loop will run again
  // and try a new number
}
轻许诺言 2025-02-04 09:00:55

您已经接近了,只需随机选择,直到新选秀与上一选菜不同。

int lastPick = -1;
int randomPick = -1;

if (checkBox1.Checked == true)
{
    while (randomPick == lastPick)
    { 
        randomPick = rnd.Next(pictureBoxArray.Length); 
    }
    lastPick = randomPick;
    PictureBox picBox = pictureBoxArray[randomPick];
    picBox.BorderStyle = BorderStyle.FixedSingle;
}

You are close, just pick randomly until the new pick is not the same as the previous one.

int lastPick = -1;
int randomPick = -1;

if (checkBox1.Checked == true)
{
    while (randomPick == lastPick)
    { 
        randomPick = rnd.Next(pictureBoxArray.Length); 
    }
    lastPick = randomPick;
    PictureBox picBox = pictureBoxArray[randomPick];
    picBox.BorderStyle = BorderStyle.FixedSingle;
}
忆伤 2025-02-04 09:00:55

由于循环时使用了其他答案,因此我想提出一种在没有一段时间循环的情况下进行此操作的方法。创建初始化的索引列表,以将所有可能的索引包含到您的数组中。此解决方案需要System.linq

将您以前选择的索引初始化为-1。

int lastChosenIndex = -1;

在您的数组中创建所有可能的索引的列表。

List<int> indicesList = Enumerable.Range(0, pictureBoxArray.Length).ToList();

现在,当您想要索引到数组中时,您可以从索引列表中获取索引。

var randomIndex = random.Next(indicesList.Count - 1);
var randomItem = pictureBoxArray[indicesList[randomIndex]];

我们将从索引列表中删除此选择的索引,以便再次选择它。首先,我们需要添加先前删除的索引(如果不是-1),因为它现在是有效的选择。

if (lastChosenIndex > -1)
    // Use Add so the index into this list doesn't change position
    indicesList.Add(lastChosenIndex); 

lastChosenIndex = indicesList[randomIndex];

// by removing the index at this position, there is no way to choose it a second time
indicesList.RemoveAt(randomIndex);

好处是,如果您想从未显示副本,则可以删除最后选择的索引代码,并且永远不会显示重复。与其他答案相比,这有点长一点,但是想证明还有一种替代与一段时间循环使用蛮力的替代方案。

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.

int lastChosenIndex = -1;

Create a list of all possible indices into your array.

List<int> indicesList = Enumerable.Range(0, pictureBoxArray.Length).ToList();

Now when you want an index into your array, you get the index from the indices list.

var randomIndex = random.Next(indicesList.Count - 1);
var randomItem = pictureBoxArray[indicesList[randomIndex]];

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.

if (lastChosenIndex > -1)
    // Use Add so the index into this list doesn't change position
    indicesList.Add(lastChosenIndex); 

lastChosenIndex = indicesList[randomIndex];

// by removing the index at this position, there is no way to choose it a second time
indicesList.RemoveAt(randomIndex);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文