首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

施用JAVA中的随机函数产生不重复的数

2012-08-08 
使用JAVA中的随机函数产生不重复的数!Random rd new Random()int i rd.NextInt(100)+1 public class

使用JAVA中的随机函数产生不重复的数!
Random rd = new Random();
int i = rd.NextInt(100)+1


public class RandomTest
{
private static final int SET_SIZE = 6;
public static final int RANDOM_MAX_VALUE = 33;
private static Random random = new Random();
public static void main(String[] args)
{
Set set = new HashSet();
while (set.size() < SET_SIZE )
{
Integer integer =new Integer(random.nextInt(RANDOM_MAX_VALUE) );
set.add(integer);
}

Iterator it = set.iterator();
while(it.hasNext())
{
System.out.println("Output A ball:"+it.next());
}
int i;
i=random.nextInt(16)+1;
System.out.print("Output B ball:"+i);
}
}

热点排行