从卡片的图像列表中提取5个不同的C#值

从卡片的图像列表中提取5个不同的C#值,c#,random,C#,Random,我有5个单独的图像列表,它们都需要提取5个彼此不同的值。我一直在尝试几种不同的方法,但没有找到一种有效的方法。该计划应该是一个扑克手,所以他们都需要不同的卡 这就是我所拥有的。(由于不工作,删除了一些)。我需要做什么才能获得这5张不同的卡 namespace Random_Card { public partial class Form1 : Form { public Form1() { InitializeCompone

我有5个单独的图像列表,它们都需要提取5个彼此不同的值。我一直在尝试几种不同的方法,但没有找到一种有效的方法。该计划应该是一个扑克手,所以他们都需要不同的卡

这就是我所拥有的。(由于不工作,删除了一些)。我需要做什么才能获得这5张不同的卡

namespace Random_Card
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void getCardButton_Click(object sender, EventArgs e)
        {
            Random rand = new Random();

            int index = rand.Next(cardImageList.Images.Count);
            int index1 = rand.Next(cardImageList.Images.Count);
            int index2 = rand.Next(cardImageList.Images.Count);
            int index3 = rand.Next(cardImageList.Images.Count);
            int index4 = rand.Next(cardImageList.Images.Count);

            cardPictureBox.Image = cardImageList.Images[index];
            cardPictureBox1.Image = cardImageList.Images[index1];
            cardPictureBox2.Image = cardImageList.Images[index2];
            cardPictureBox3.Image = cardImageList.Images[index3];
            cardPictureBox4.Image = cardImageList.Images[index4];



            while (index == index1)
            {
                new Random();
            }
        }
    }
}

首先,要使用
Random
,您应该只为类创建一个实例并初始化它一次。否则,如果在循环中调用,您可能会从它们那里得到相同的数字,因为它是由系统时钟初始化的

其次,如果您想从一个列表中选择五个唯一的索引,您可以简单地创建一个包含所有索引的列表,将它们洗牌,然后从中选择前五个索引

在下面的代码中,我使用
Enumerable.Range(0,cardImageList.Images.Count)
来获得编号的顺序列表,从
0开始。这是简化的语法,可以用
for
循环替换。接下来,我使用
System.Linq
扩展方法
OrderBy
对索引列表进行排序,并将
Random.NextDouble()
传递给它。这使得它以随机的方式排列列表;基本上是一种简单的“洗牌”列表的方法。索引列表被洗牌后,我使用
Take()
扩展方法从列表中获取前五项:

namespace Random_Card
{
    public partial class Form1 : Form
    {
        private Random rand = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void getCardButton_Click(object sender, EventArgs e)
        {
            // Get five unique random indexes
            List<int> shuffledIndexes = Enumerable.Range(0, cardImageList.Images.Count)
                .OrderBy(x => rand.NextDouble()).Take(5).ToList();   

            cardPictureBox.Image = cardImageList.Images[shuffledIndexes[0]];
            cardPictureBox1.Image = cardImageList.Images[shuffledIndexes[1]];
            cardPictureBox2.Image = cardImageList.Images[shuffledIndexes[2]];
            cardPictureBox3.Image = cardImageList.Images[shuffledIndexes[3]];
            cardPictureBox4.Image = cardImageList.Images[shuffledIndexes[4]];          
        }
    }
}
namespace随机卡
{
公共部分类Form1:Form
{
private Random rand=new Random();
公共表格1()
{
初始化组件();
}
私有void getCardButton_单击(对象发送者,事件参数e)
{
//获得五个唯一的随机索引
List ShuffledIndex=Enumerable.Range(0,cardImageList.Images.Count)
.OrderBy(x=>rand.NextDouble()).Take(5.ToList();
cardPictureBox.Image=cardImageList.Images[ShuffledIndex[0]];
cardPictureBox1.Image=cardImageList.Images[ShuffledIndex[1]];
cardPictureBox2.Image=cardImageList.Images[ShuffledIndex[2]];
cardPictureBox3.Image=cardImageList.Images[ShuffledIndex[3];
cardPictureBox4.Image=cardImageList.Images[ShuffledIndex[4]];
}
}
}

更新:您提到您不知道如何在循环中执行此操作,因此这里有一个“长手”方法的示例来执行相同的操作,这可能更有意义

var shuffledIndexes = new List<int>();

// Get a sequential list of integers representing the indexes
for (int i = 0; i < cardImageList.Images.Count; i++)
{
    shuffledIndexes.Add(i);
}

// Shuffle the items in the index list by randomly swapping each item with another
for (int i = 0; i < cardImageList.Images.Count; i++)
{
    // Pick a random item to swap with
    var swapIndex = rand.Next(shuffledIndexes.Count);

    // If we picked the current item, continue without doing anything
    if (swapIndex == i) continue;

    // Otherwise, swap the current item with the random item
    var temp = shuffledIndexes[i];
    shuffledIndexes[i] = shuffledIndexes[swapIndex];
    shuffledIndexes[swapIndex] = temp;
}

// Assign the images from the first five shuffled indexes
cardPictureBox.Image = cardImageList.Images[shuffledIndexes[0]];
cardPictureBox1.Image = cardImageList.Images[shuffledIndexes[1]];
cardPictureBox2.Image = cardImageList.Images[shuffledIndexes[2]];
cardPictureBox3.Image = cardImageList.Images[shuffledIndexes[3]];
cardPictureBox4.Image = cardImageList.Images[shuffledIndexes[4]];
var shuffledIndex=new List();
//获取表示索引的整数的顺序列表
对于(int i=0;i
请阅读并记录。当一个网络组件看起来不工作时——运行,不要闲逛,去MSDN确认你正确使用它。另外,从列表中随机挑选牌与洗牌和发牌完全不同。随机选取不会排除重复/重复。对于一个类,您只需要一个
Random
对象实例,它应该初始化一次。并且您需要添加一个数据结构来跟踪您要处理的卡索引,因此当相同的随机数再次出现时,重复随机选取,直到命中可用的卡索引(坏主意)或使用随机排序顺序将卡片列表预排序到队列或堆栈中(更好的主意)。如果您试图模拟一副牌,那么请编写一副牌的模拟。制作一个表示牌的数据结构,制作一个表示牌的数据结构,并编写从牌中洗牌和处理的操作。编程是制作表示概念的抽象的艺术,所以开始吧。我尝试了修复正在初始化它并抛出一个错误。_italic_rand给了我一个问题,它说“名称空间不能直接包含字段或方法之类的成员”。我也尝试过如何在for循环中实现该代码,但不知道如何实现。目前我正在学习C#,所以有很多我不知道的。只是尝试做项目。抱歉,这是我的错…类中应该有Random,而不是名称空间。我现在已经修复了它。没问题。我添加了另一个使用
for
循环创建的示例索引的列表,并对列表进行无序排列。这可能更容易理解。