random.nextInt()%100就可以得到100以内的随机值吧。。还是其他方法吗

解决方案 »

  1.   

    Random r = new Random();
      

  2.   

    public class Test {
            
        /**
         * Creates a new instance of <code>Test</code>.
         */
        public Test() {
        }
        
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
         while(true)
           System.out.println ((int)(Math.random()*100));
        }
    }
      

  3.   

    Random random = new Random();
    random.nextInt(100);
      

  4.   

    nextInt()方法会产生负数,所以要先把负数去掉
    Random rm=new Random();
    do{
    int x=rm.nextInt();
    }while(x<0)
    int r=x%101;
    System.out.println(""+r);
      

  5.   

    Math.random()生成double型的转下。或者自己写下,任意产生一个数,除以你要生成的随机数,那么余数肯定小于这个数