扑克牌洗牌算法

package com.citi.byteman.test.arithmatic;

import java.util.Random;

public class XiPai {

	private String[] singleCard = { "2", "3", "4", "5", "6", "7", "8", "9",
			"10", "J", "Q", "K", "A" };
	private String[] allCards = new String[54];

	// shuffle 100 times, exchange two cards every time
	public void shuffleCards() {
		allCards = initCard();
		String temp = "";
		for (int i = 0; i < 100; i++) {
			int m = new Random().nextInt(54);
			int n = new Random().nextInt(54);
			temp = allCards[m];
			allCards[m] = allCards[n];
			allCards[n] = temp;
		}

	}

	// initial all 54 cards
	public String[] initCard() {
		for (int i = 0; i < singleCard.length; i++) {
			String[] tempCards = addMsg(singleCard[i]);
			for (int j = 0; j < 4; j++) {
				allCards[i * 4 + j] = tempCards[j];
			}
		}
		allCards[52] = "Big Joke";
		allCards[53] = "Small Joke";
		return allCards;
	}

	private String[] addMsg(String card) {
		return new String[] { "Black-" + card, "Red-" + card, "Plum-" + card,
				"Box-" + card };
	}

	public void printCurrentCards() {
		for (int i = 0; i < allCards.length; i++) {
			System.out.println(allCards[i]);
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		XiPai xipai = new XiPai();
		xipai.shuffleCards();
		xipai.printCurrentCards();
	}

}


算法思想: 两个随机数对应扑克牌位置去相互交换
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值