代码:public class RandomCount {
public static void main(String [] args)
{
Random rd=new Random(20);
int temNum;
SortedMap<Integer,Integer> sm=new TreeMap<Integer,Integer>();
for(int i=0;i<10;i++)
{
temNum=rd.nextInt(31);
if(sm.containsKey(temNum))
{
sm.put(temNum, sm.get(temNum)+1);
}else
{
sm.put(temNum, 1);
}
}
System.out.println(sm);
}
}
这题的目的是:循环10次找出31以类的数中,最常出现的数,并对它统计。
我在我机子上,每次运行的结果都是:
{2=1, 8=2, 11=1, 12=1, 14=1, 16=1, 18=1, 23=1, 27=1}我认为他的键每次运行都应该不同(Random获取的数)才对。请各位帮我看看我代码是不是有问题,还是什么原因导致每次运行结果都一样?
public static void main(String [] args)
{
Random rd=new Random(20);
int temNum;
SortedMap<Integer,Integer> sm=new TreeMap<Integer,Integer>();
for(int i=0;i<10;i++)
{
temNum=rd.nextInt(31);
if(sm.containsKey(temNum))
{
sm.put(temNum, sm.get(temNum)+1);
}else
{
sm.put(temNum, 1);
}
}
System.out.println(sm);
}
}
这题的目的是:循环10次找出31以类的数中,最常出现的数,并对它统计。
我在我机子上,每次运行的结果都是:
{2=1, 8=2, 11=1, 12=1, 14=1, 16=1, 18=1, 23=1, 27=1}我认为他的键每次运行都应该不同(Random获取的数)才对。请各位帮我看看我代码是不是有问题,还是什么原因导致每次运行结果都一样?
不写就用当前时间当随机因子 每次就不同了Random rd=new Random()