生成6位数字组合的随机数

方法1

  public int nextInt(final int min, final int max)

  {

  Random rand= new Random();

  int tmp = Math.abs(rand.nextInt());

  return tmp % (max - min + 1) + min;

  }

  方法2:不会生成小于100000的数

  Random random = new Random();

  int x = random.nextInt(899999);

  int x = x+100000;

  方法3

  int n = 0 ;

  while(n < 100000){

  n = (int)(Math.random()*1000000);

  }

  System.out.println(n);

  方法4

  public static void f(){

  String str = "";

  str += (int)(Math.random()*9+1);

  for(int i = 0; i < 5; i++){

  str += (int)(Math.random()*10);

  }

  int num = Integer.parseInt(str);

  System.out.println(num);

  }

  方法5

  System.out.println((int)((Math.random()*9+1)*100000));

  方法6

  Random random = new Random();

  String result="";

  for(int i=0;i<6;i++){

  result+=random.nextInt(10);

  }

  System.out.print(result);

你可能感兴趣的:(java相关)