C#:我应该如何改组数组的内容? [英] C#: How should I go about shuffling contents of an array?

查看:130
本文介绍了C#:我应该如何改组数组的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个称为cards [52]的整数数组.每个元素都包含一个代表纸牌的整数(例如,cards [0] = 5将代表6个俱乐部).

I have an array of integers called cards[52]. Each element contains an integer that represents a card (cards[0] = 5 would represent the 6 of clubs for example).

这就是我重新排列数组的方式.可以,但是我会重复.

This is how I shuffled the array. It works, but I am getting repeats.

private void shuffleCards()
    {
        for (int i = 0; i < cards.Length; i++)
        {
            int randIndex = r.Next(0, 52);
            int origCard = cards[i];
            cards[i] = cards[randIndex];
            cards[randIndex] = origCard;
        }
    }

r是我在程序开头初始化的随机变量.

r is the Random variable I initialized in the beginning of the program.

在测试程序时,我注意到我会得到2到3次相同的卡.显然,我无法重复,它们都必须是不同的.我无法以任何方式看到这种方法如何使我重复.

When testing the program, I noticed I would get the same card 2 or 3 times. Obviously I cannot get repeats, they all must be different. I can't see in any way how this method gives me repeats.

如何无序重复此数组?谢谢.

How can I shuffle this array without any repeats? Thanks.

编辑

好吧,事实证明随机播放功能不是问题.导致问题的原因是deal()函数.

Okay, turns out the shuffle function wasn't the problem. It is the deal() function that is causing the problem.

private void deal()
    {
        for (int i = 0; i < 26; i++)
        {
            userCards[i] = cards[i];
            setUserValue(); //ignore this
        }
        for (int i = 26; i < 52; i++)
        {
            opponentCards[i - 26] = cards[i];
            setOpponentValue(); //ignore this
        }
    }

我确定这不是随机播放功能.我将所有卡的结果打印到一个文本文件中,没有看到任何迭代.但是,当将牌发给用户和对手时,即所有的重复都发生了.有什么建议吗?

I know for sure it isn't the shuffle function. I printed the results of all the cards to a text file and saw no iterations. However, when the cards are dealt to the user and the opponent, that's when all the iterations occur. Any advice?

推荐答案

有一种非常简单的算法,称为费舍尔·耶茨混洗算法,可在O(n)时间和O(1)空间复杂度中进行混洗.

There is very simple algorithm known as Fisher Yates shuffling algorithm which does the shuffling in O(n) time and O(1) space complexity.

使用随机函数从给定的集合中生成一个随机索引,并用最后一个索引替换生成的随机元素.递减最后一个索引,并像这样继续其余元素.

Use a random function that generates a random index from the given set and replace the random element generated with the last index. Decrement last index and continue like this for the rest of elements.

让大小为n的数组为:arr[n]

Let the array of size n be : arr[n]

void randomize ( int arr[], int n )
{
    // Use a different seed value so that we don't get same
    // result each time we run this program
    srand ( time(NULL) );

    // Start from the last element and swap one by one. We don't
    // need to run for the first element that's why i > 0
    for (int i = n-1; i > 0; i--)
    {
        // Pick a random index from 0 to i
        int j = rand() % (i+1);

        // Swap arr[i] with the element at random index
        swap(&arr[i], &arr[j]);
    }
}

来源:算法

这篇关于C#:我应该如何改组数组的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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