如下:
情况一:
一维数组:int[] Array1=new int[10]{1,2,3,4,5,6,7,8,9,10};二维数组:int[,] Array2=new int[2,10]{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};或是更多维数组我想要的结果:Array1随机化:{2,5,8,3,1,4,6,7,0,9}
Array2随机化:{2,5,8,3,1,4,6,7,0,9,12,15,18,13,11,14,16,17,10,19}情况二:ArrayList[] Array1=new ArrayList();
Array1.Add(1);
Array1.Add(2);
Array1.Add(3);
Array1.Add(4);
Array1.Add(5);
Array1.Add(6);
Array1.Add(7);
Array1.Add(8);
Array1.Add(9);
Array1.Add(10);或是更数我想要的结果:Array1 链表 随机化 内容 顺序 :{2,5,8,3,1,4,6,7,0,9}或是C#.Net 中 有没有哪个类的哪个函数可以达到这种随机化效果?我可不想用Random 去将每个数的位置去随机化,而且,如果又重复的话,又要继续下一个随机,那就很麻烦了。

解决方案 »

  1.   

    for循环10次,每次循环两个下标,然后这两个下标地址的数据对换一下就好了
      

  2.   

    for循环10次,每次循环两个下标,然后这两个下标地址的数据对换一下就好了
      

  3.   

    int[] card = {...};// shuffle
    Random random = new Random();
    for (int i = cards.Length - 1; i > 0; i--)
    {
        int j = random.Next(i);
        int swap = cards[i];
        cards[i] = cards[j];
        cards[j] = swap;
    }
    引用自:
    http://topic.csdn.net/u/20081215/14/91fde064-f80d-4493-bf5e-1a576a53744a.html
      

  4.   

    随机的无序状态肯定是用Random 
    不存在重复的问题了 遍历数组 对每个元素随机一个0-9的数 然后把此元素跟这个下标的元素互换就行了
      

  5.   

                int[] Array1 = new int[10] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
                int tmp = 0;
                Random rand = new Random();
                for (int a = 0; a <= 10; a++)
                {
                    int i = rand.Next(0, 10);
                    int j = rand.Next(0, 10);
                    tmp = Array1[i];
                    Array1[i] = Array1[j];
                    Array1[j] = tmp;
                }
      

  6.   

    上面提到算法都可以哦。不过他们用到的都是.NET自带的Random类,这种随机产生的随机数可以重复出现,所以很多时候都是得到的可能是相同的。
    我前不久刚写了一个类似的全排列随机生成类库,随机性很好的,是应在密码学算法当中,如果你想用的话,我可以提供给你dll,代码就不提供了。直接调用就可以。.NET中肯定没有这样的算法啊
      

  7.   

    如果改用System.Security.CryptoAlgorthim下面的 RandomNumberGenerator产生随机数,然后用上面兄弟的两两交换,效果应该就不错了可以把交换的轮数设置为随机的,更好。
      

  8.   


    感谢2楼4楼5楼6楼朋友的算法介绍。不过我与出来的虽然不占用CPU,但要人为间隔时间去生成随机数,所以洗牌需要要时间。不知道你们写的会不会。有没有无顺序算法,又快,又有效率。因为没有人为的Thread.Sleep。Random 的DateTime.Now.Tick间隔太少,一般都是生成相同的数。
      

  9.   

    See my Code...就是要有人为的Thread.Sleep,就这点不好。不知道有没有很好的非人为又快速的随机数。        private void SubShuffle()
            {
                _isShuffling = true;
                for (int i = 0; i < _theArryListCards.Count; i++)
                {
                    int index1 = new Random().Next(i);
                    Thread.Sleep(5);
                    int index2 = new Random().Next(_theArryListCards.Count - 1);
                    Thread.Sleep(5);
                    Card tempVar = _theArryListCards[index1] as Card;
                    _theArryListCards[index1] = _theArryListCards[index2];
                    _theArryListCards[index2] = tempVar;
                }
                for (int i = 0; i < _theArryListCards.Count; i++)
                {
                    _theCards[i] = _theArryListCards[i] as Card;
                }
                _isShuffling = false;
            }
      

  10.   

    大哥啊 跟你说了啊。用这个类可以产生随机数,产生的肯定不同啊:RandomNumberGenerator。
    random的时间间隔小了,产生的随机数就会相同的
      

  11.   


    把Random的构造(那个new Random)放到循环外面不就可以了?
      

  12.   

    .NET提供了一个这样的类:RandomNumberGenerator,在system.security.cryptograhy命名空间下,产生的随机数一定不会相同的啊。比Random好多了,具体用法你查查MSDN就知道了。。
      

  13.   


    你的意思是:        private void SubShuffle()
            {
                _isShuffling = true;
                int index1 = new Random().Next(_theArryListCards.Count);
                //Thread.Sleep(5);
                int index2 = new Random().Next(_theArryListCards.Count);
                for (int i = 0; i < _theArryListCards.Count; i++)
                {                //Thread.Sleep(5);
                    Card tempVar = _theArryListCards[index1] as Card;
                    _theArryListCards[index1] = _theArryListCards[index2];
                    _theArryListCards[index2] = tempVar;
                }
                for (int i = 0; i < _theArryListCards.Count; i++)
                {
                    _theCards[i] = _theArryListCards[i] as Card;
                }
                _isShuffling = false;
            }什么反应都没有。
      

  14.   


    报歉,之前没有认真看您的代码思路。现在改好了,432张牌,也150ms左右搞定了,很好.
    分享给你:        /// <summary>
            /// 交换牌
            /// </summary>
            private void SwapCards()
            {
                Random r1 = new Random();
                Random r2 = new Random();
                Random r3 = new Random();
                int swapCount = new Random().Next(1, 3);
                for (int x = 0; x < swapCount; x++)
                    for (int i = 0; i < _theArryListCards.Count; i++)
                    {
                        int index1 = 0;
                        int index2 = 0;
                        int randomSeed = r3.Next(2, 4);
                        switch (randomSeed)
                        {
                            case 2:
                                index1 = r1.Next(i);
                                index2 = r2.Next(_theArryListCards.Count);
                                break;
                            case 3:
                                index1 = r1.Next(i);
                                index2 = r2.Next(_theArryListCards.Count - 1 - i, _theArryListCards.Count);
                                break;
                            case 4:
                                index1 = r1.Next(_theArryListCards.Count / 2, ((_theArryListCards.Count / 2) + _theArryListCards.Count / 3));
                                index2 = r2.Next(((_theArryListCards.Count / 2) - _theArryListCards.Count / 3), _theArryListCards.Count / 2);
                                break;
                            default:
                                break;
                        }
                        Card tempVar = _theArryListCards[index1] as Card;
                        _theArryListCards[index1] = _theArryListCards[index2];
                        _theArryListCards[index2] = tempVar;
                    }
            }
      

  15.   

    54000=5.4W张牌。生成牌时要15s洗牌也要15s看来效果还不错。