JAVA four randomly generated numbers

Java generates a random number generation common way:

1, using the Random class, the following manner may generate an integer of 0 to 10 000, 10 000 is not included, but not necessarily four-digit

Random random = new Random();
System.out.println("---------"+random.nextInt(10000) );

2、Math.random()*9000+1000  或 (int)((Math.random()*9+1)*1000)

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

   Description:

   Math.random () is in the range [0,1)
   math.random range () is 9000 * [0,9000);
   math.random range () * 9000 + 1000 is [1000,10000 ).

 

Remarks:

     1, those pit Random, as the result will always be a value

      for(int i =0 ;i<50;i++){
Random random = new Random(10000);
System.out.println("-----------"+random.nextInt(10000) );
}
Output: 
----------- 2208
----------- 2208
----------- 2208
----------- 2208
----------- 2208
----------- 2208
----------- 2208
----------- 2208
- 2208 ----------
----------- 2208

Guess you like

Origin www.cnblogs.com/xuzhujack/p/12630101.html