纯随机数发生器

public class Suijishu   

  public static void main(String[] args) {  

    int n=1000;//n是生成随机数的个数

    Creat(n);

  }
  static BigInteger Creat(int n) {//生成随机数
    BigInteger result;//生成的随机数
    if(n==1) {//生成第一个随机数,由于没有Creat(n-1),所以用Math.random();
      result=BigInteger.valueOf((int)Math.random()*100000+1);
      System.out.println("第1个随机数是"+result);
      return result;
    }
    else {
      BigInteger i=Creat(n-1).multiply(BigInteger.valueOf(16807));
      result=i.mod(BigInteger.valueOf(Integer.MAX_VALUE));//随机数等于Creat(n-1)*16807%int.MAX_VALUE
      System.out.println("第"+n+"个随机数是"+result);
      return result;
    }
  }

}

猜你喜欢

转载自www.cnblogs.com/lianggegege123/p/11599890.html