如何产生20-9999之间的随机数

解决方案 »

  1.   

    import java.util.Random;/**
     * @author mengfanpp
     *
     */
    public class SuiJiShu { private static int getRandom() {
    Random random = new Random();
    int rValue = Math.abs(random.nextInt()) % 10000; // 保证在0-9999之间
    if (rValue < 20) {
    rValue = Math.abs(random.nextInt()) % 10000;
    }
    return rValue; } /**
     * @param args
     */
    public static void main(String[] args) {
    // TODO Auto-generated method stub
    for (int i = 0; i < 1000; i++) {
    System.out.println(getRandom());
    }
    }}
      

  2.   

    public class Test
    {
    public static void main(String[] args)
    {
    for  ( int i = 0; i<100; i++ ) 
    {
    int random = (int)(Math.random()*9999);
    if ( random > 20 )
    System.out.println(random);
    }


    }
    }
      

  3.   

    public class Test
    {
    public static void main(String[] args)
    {
    int random = 20 + (int)(Math.random()*9980);
    System.out.println(random);
    }
    }
    这样做就可以了