导航

洗牌算法

Posted on 2009-11-17 14:13  杨彬Allen  阅读(196)  评论(0)    收藏  举报
    /// <summary>
    
/// 洗牌算法:先給一個一維數組賦值1~10,然後隨即打亂這個數組。最簡單得就是用冒泡算法來打亂。
    
/// </summary>
    public void XiPaiSuanFa()
    {
        
int[] cards = new int[10] { 12345678910 };
        Random ram 
= new Random();
        
int currentIndex;
        
int tempValue;
        
for (int i = 0; i < 10; i++)
        {
            currentIndex 
= ram.Next(010 - i);
            tempValue 
= cards[currentIndex];
            cards[currentIndex] 
= cards[9 - i];
            cards[
9 - i] = tempValue;
        }
    }