用Random求0-10的随即数 Random a1=new Random();

for(int a=0;a<10;a++){


System.out.println(a1.nextInt());

}
我才学没多久,请大家指教是哪里错了,谢谢!!

解决方案 »

  1.   

    用Random求0-10的随即数 
    Random a1=new Random(); for(int a=0;a <10;a++){ 
    System.out.println(a1.nextInt(10)); } 
    加个10可以了。
      

  2.   

    概率? 10% 大致是什么呢》next(10) == 2 我想这个就是10%吧!
    next(5) == 2 我想是20%
      

  3.   

    利用Random,按照概率分别为10% 20% 30% 40%取得1,2,3,4 
      

  4.   

    答:很简单啊。代码如下:Random r=new Random(System.currentTimeMillis()); 
    int v=0;
    for(int i=1;i<=10;i++){
    int v1=r.nextInt(10)+1;//产生1-10之间的一个随机数
    switch(v1)
    {
    case 1:         v=1;break;//10%概率出现数字1
    case 2:case 3:  v=2;break;//20%概率出现数字2
    case 4:case 5:
    case 6:  v=3;break;//30%概率出现数字3
    default:        v=4;break;//40%概率出现数字4
    }//其它数字不可能了。因为剩下的概率是0了。
               System.out.print(v+" ");//打印出这个概率数字
      

  5.   

    Random rand=new Random(); 
    while(true) {
    int x = rand.nextInt(4);
    int y = rand.nextInt(4);
    if (x>=y) System.out.println(x + 1);
    }
      

  6.   

    还是楼主的方法好!!! 简单就是美啊!!!Random r=new Random(System.currentTimeMillis()); 
            int v=0;
            for(int i=1;i<=10;i++){
                int v1=r.nextInt(10);//产生1-10之间的一个随机数
                switch(v1)
                {
                case 0:         v=1;break;//10%概率出现数字1
                case 1:case 2:  v=2;break;//20%概率出现数字2
                case 3:case 4:
                case 5:         v=3;break;//30%概率出现数字3
                default:        v=4;break;//40%概率出现数字4
                }//其它数字不可能了。因为剩下的概率是0了。
               System.out.print(v+" ");//打印出这个概率数字
             }