洗牌算法java

1.Fisher-Yates Shuffle

    // 从所有排列表中第i张后面的牌中随机选出一张作为前牌中的第i张
    // 改变了牌列表的顺序
    public void FYShuffle(int size)
    {
        int tempInd;
        for(int i = 0; i < size; i++)
        {
            tempInd = i + rdm.nextInt(size - i);// 获得i后的随机数
            curList[i] = new Card(cardList[tempInd]);// 将选出后的牌赋给curList[i]
            this.swap(cardList[i], cardList[tempInd]);// 将选出后的牌放在cardList[i]
        }

    }

2.Knuth-Durstenfeld Shuffle

    // 将前面的i张牌中随机选出一张和第i张交换位置
    // 节省了空间
    public void KDShuffle(int size)
    {
        int tempind;
        this.setCurList(this.getCardList());
        for(int i = size - 1; i > 0; i--)
        {
            tempind = rdm.nextInt(i);
            this.swap(curList[i], curList[tempind]);

        }

    }

3.Inside-Out Algorithm

    // 在前面的i-1张牌中随机选出一张和第i张牌交换。
    public void IOShuffle(int size)
    {
        int tempInd;
        this.setCurList(this.getCardList());
        for(int i = 1; i < size; i++)
        {
            tempInd = rdm.nextInt(i);
            // curList[tempInd] = curList[i];
            // curList[i] = cardList[tempInd];
            this.swap(curList[i], curList[tempInd]);
        }

    }

原文在这里:点击打开链接

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值