Random rand = new Random(System.currentTimeMillis());
rand.nextInt() % 3; // -2 -1 0 1 2
rand.nextInt() % 2; // -1 0 1

解决方案 »

  1.   

    没有办法用nextDouble(),因为1.0是不包括的,只好找了下面一条变通的办法,你可以试试.
    int digits = 3;
    (rand.nextInt() % ( 3 * Math.pow(10,digits) + 1) ) / Math.pow(10,digits)
      

  2.   

    呵呵,谢谢你!我做了个测试,确实没问题
    public static void main(String[] args) 
    {
    int digits = 3;
    Random rand = new Random(System.currentTimeMillis());
    for (int i=0;i<20000;i++)
    {
    double asdf = (rand.nextInt() % ( 2 * Math.pow(10,digits) + 1) ) / Math.pow(10,digits);
    if(asdf==2.0)
    {
    System.out.println("OK");
    break;
    }
    }
    }