是否有可能不显示列表中的上一个随机项? [英] Is it possible to don't display the previous random item from a list?

查看:63
本文介绍了是否有可能不显示列表中的上一个随机项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

就像问题所说的那样,是否有可能不显示列表中的上一个随机项?基本上我有一个清单:



Like the question says, is it possible to not display the previous random item from a list? basically I have a List:

List<Animal> animals = new List();





具有以下属性< br $> b $ b



That have the following properties

public string Name
{
    get { return _name; }
    set { _name = value; }
}

public string Specie
{
    get { return _specie; }
    set { _specie = value; }
}





我尝试过:



然后将这个对象名或物种随机生成到文本框:





What I have tried:

This object name or specie is then being randomly generated to text boxes:

private string RandomAnimal(Random r)
{
    if (random.Next(2) == 0)
    {
        txtSpecie.Text = randomAnimal.Specie;
        return randomAnimal.Specie;
    }
    else
    {
        txtName.Text = randomAnimal.Name;
        return randomAnimal.Name;
    }
}





假设随机动物名称已显示在文本框中。是否有可能编写一种方法,可能会确保以前在文本框中显示的名称不再显示?



提前谢谢



Lets say that random animal Name has been shown on the textbox. Is it possible to write a method perhaps that will insure that the same name that was previously displayed in textbox will not be displayed anymore?

Thanks in advance

推荐答案

如果我找到了你,那么你需要避免随机选择中的重复。看看我的随机抽取甲板上的5张卡 [ ^ ]。它是 C ++ 代码,但我想你可以很容易地得到这个想法。
If I got you, then you need to avoid replicates in random selections. Have a look at my Random extraction of 5 cards from a deck[^]. It is C++ code, but I guess you could easily get the idea.


简单的解决方案?

复制列表:

Simple solution?
Copy the List:
List<Animal> takeFromHere = new List<Animal>(animals)



并将其用于随机选择。选择项目时,将其从集合中删除。如果该集合现在为空,请重新填充它。


and use that for your random selection. When you select an item, remove it from the collection. If the collection is now empty, re-fill it.


这篇关于是否有可能不显示列表中的上一个随机项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆