Java 常用类库 之 Random 随机数类实例
http://www.verejava.com/?id=169931132381103
/**
知识点:Random 随机数类
实际应用: 敌人飞机随机从屏幕的上方出现
*/
import java.util.*;
public class TestRandomCard
{
public static void main(String[] args)
{
String[] cards={"A","A","A","A",
"2","2","2","2",
"3","3","3","3",
"4","4","4","4",
"5","5","5","5",
"6","6","6","6",
"7","7","7","7",
"8","8","8","8",
"9","9","9","9",
"10","10","10","10",
"J","J","J","J",
"Q","Q","Q","Q",
"K","K","K","K",
"小王","大王"
};
System.out.println("原牌:");
for(int i=0;i<cards.length;i++)
{
System.out.print(cards[i]+",");
}
//随机打乱
Set set=new HashSet();
Random rn=new Random();
while(true)
{
set.add(rn.nextInt(54));
if(set.size()==54)
{
break;
}
}
System.out.println("\n54个随机索引号:");
Object[] iter=set.toArray();
for(int i=0;i<iter.length;i++)
{
System.out.print(iter[i]+",");
}
List zhanshan=new ArrayList();
List lisi=new ArrayList();
List wangwu=new ArrayList();
}
}