---
title: "洗扑克牌（乱数排序）"
author: "fairy_xzc"
date: "2017-02-20T20:05:21Z"
tags: []
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2017-02-20T20:05:49Z"
id: 2358123
url: "https://agilestyle.iteye.com/blog/2358123"
---

**原创转载请注明出处：[http://agilestyle.iteye.com/blog/2358123](/blog/2358123)**

 

基本思想：54张牌从小到大有序排列，产生一个0-53的随机乱数作为Index，并且与当前的值进行swap，遍历过后，54张牌就从有序变为乱序了

```
package org.fool.test;

import java.util.Random;

public class Shuffle {

	public static void main(String[] args) {
		int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
				11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 
				21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 
				31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 
				41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 
				51, 52, 53, 54 };
		
		for (int i = 0; i < a.length; i++) {
			int randomIndex = new Random().nextInt(54);
			
			int temp = a[i];
			a[i] = a[randomIndex];
			a[randomIndex] = temp;
		}
		
		printArray(a);
	}
	
	public static void printArray(int[] a) {  
        for (int i : a) {  
            System.out.print(i + " ");  
        }
        
        System.out.println();  
    }

}
```