A random number generation method
① minimum + Math.random () * (Max - Min 1) : number generated in the range [min, max]
package com.java1; import javax.swing.JOptionPane; public class RandomInt { public static void main( String args[] ) { int value; String output = ""; for ( int i = 1; i <= 20; i++ ) { value = (int) ( 1+Math.random() * 6 ); output += value + " "; if ( i % 5 == 0 ) output += "\n"; } JOptionPane.showMessageDialog( null, output, "20 Random Numbers from 1 to 6", JOptionPane.INFORMATION_MESSAGE ); System.exit( 0 ); } }
Between the range of the random number generation 1-6
② The new Random (). NextInt (26 ) approach is used to achieve a greater than or equal to 0, a random number less than 26
package com.java1; import java.util.*; public class TestRandom { public static void main(String[] args) { Random rand = new Random(); System.out.println("rand.nextBoolean():" + rand.nextBoolean()); byte[] buffer = new byte[16]; rand.nextBytes(buffer); System.out.println(Arrays.toString(buffer)); //生成0.0~1.0之间的伪随机double数 System.out.println("rand.nextDouble():" +rand.nextDouble ()); // generate a pseudorandom number float between 1.0 ~ 0.0 System.out.println ( "rand.nextFloat ():" + rand.nextFloat ()); // generating mean 0.0 the standard deviation is 1.0 gauss pseudo System.out.println ( "rand.nextGaussian ():" + rand.nextGaussian ()); // generates a pseudo-random integer in the range of long integers System.out.println ( "rand.nextInt ():" + new new . the random () the nextInt ()); // generate a pseudo-random integer between ~ 26 is 0 System.out.println ( "rand.nextInt (26 is):" + rand.nextInt (26 )); // generates a long integer in the range of pseudo-random integer System.out.println ( "rand.nextLong ():" + rand.nextLong ()); } }
Answer:
rand.nextBoolean():true
[103, -90, -76, 58, -91, -52, -23, 45, 50, 73, -43, 70, 77, -64, -48, -122]
rand.nextDouble():0.1631898380376655
rand.nextFloat():0.1340416
rand.nextGaussian():-1.8408133377651577
rand.nextInt():-382427923
rand.nextInt(26):3
rand.nextLong():-1029091829791802274
③ by generating a different seed Random object , and the current time as the seed to generate Random object
package com.java1; import java.util.Random; public class TestSeed { public static void main(String[] args) { Random r1 = new Random(50); System.out.println("第一个种子为50的Random对象"); System.out.println("r1.nextBoolean():\t" + r1.nextBoolean()); System.out.println("r1.nextInt():\t\t" + r1.nextInt()); System.out.println("r1.nextDouble():\t" + r1.nextDouble()); System.out.println("r1.nextGaussian():\t" + r1.nextGaussian()); System.out.println("---------------------------"); Random r2 = new Random(50); System.out.println("第二个种子为50的Random对象"); System.out.println("r2.nextBoolean():\t" + r2.nextBoolean()); System.out.println("r2.nextInt():\t\t" + r2.nextInt()); System.out.println("r2.nextDouble():\t" + r2.nextDouble()); System.out.println("r2.nextGaussian():\t" + r2.nextGaussian()); System.out.println("---------------------------"); Random r3 = new Random(100); System.out.println("种子为100的Random对象"); System.out.println("r3.nextBoolean():\t" + r3.nextBoolean()); System.out.println("r3.nextInt():\t\t" + r3.nextInt()); System.out.println("r3.nextDouble():\t" + r3.nextDouble()); System.out.println("r3.nextGaussian():\t" + r3.nextGaussian()); Random r4 = new Random(System.currentTimeMillis()); System.out.println ( "current time as the seed Random object"); System.out.println("r3.nextBoolean():\t" + r4.nextBoolean()); System.out.println("r3.nextInt():\t\t" + r4.nextInt()); System.out.println("r3.nextDouble():\t" + r4.nextDouble()); System.out.println("r3.nextGaussian():\t" + r4.nextGaussian()); } }
Output:
The first seed for the Random object 50
r1.nextBoolean (): to true
r1.nextInt (): -1727040520
r1.nextDouble (): .6141579720626675
r1.nextGaussian (): 2.377650302287946
------------ ---------------
the second seed Random object 50
r2.nextBoolean (): to true
r2.nextInt (): -1,727,040,520
r2.nextDouble (): 0.6141579720626675
r2.nextGaussian ( ): 2.377650302287946
---------------------------
seed Random object 100
r3.nextBoolean (): to true
r3.nextInt (): - 1,139,614,796
r3.nextDouble (): .19497605734770518
r3.nextGaussian (): .6762208162903859
the current time as the seed Random object
r3.nextBoolean (): to true
r3.nextInt (): 1678327555
r3.nextDouble (): .16760365386964127
r3.nextGaussian(): -0.7219839638959297
From the results we can know: the same random number seed is produced by the same random number generated at the current time as the seed must be unique.
④ handwritten code to implement random number 1000
According to the formula:
Wherein X- n- seed input = number
c=0; a=75=16807; m=Integer.MAX_VALUE;
package com.randseed; import java.lang.*; import java.util.Scanner; public class randseed { public static void rand(long x) { long a; for(int i=1;i<=1000;i++) { a=(x*16807)%Integer.MAX_VALUE;//由公式得 System.out.println(a); x=a; } } public static void main(String[] args) { In Scanner=new new Scanner (the System.in); System.out.println ( "Please enter the seed number:" ); Long SEED; SEED = in.nextLong (); RAND (SEED); } }
1000 outputs the random number.
Second, static imports
By static import will not have to call: class name method name, but converted to direct the use of the method name invoked.
Here are three ways to solve the problem of absolute value abs
Package com.randseed; Import static java.lang.Math *. ; public class importstatic { public static void Abs ( int A) { IF (A <0 ) A = - A; System.out.println (A); } public static void main (String [] args) { // the TODO Auto-Generated Stub method System.out.println (ABS (-100)); // statically introduced, it is possible to use the direct method System.out.println (Math. ABS (-100)); // mathematical functions Bunsen is a static method Abs (-100);// custom a static approach } // three results are the same }
Three outputs are: 100
Third, the method of the variable parameter
A method using a variable parameter calculating maximum incoming parameters.
package com.java1; import java.awt.*; import java.awt.event.*; import java.util.*; public class VariableArgumentsTest{ public static double max(double...values) { double largest=Double.MIN_VALUE; for (double v:values) if(v>largest) largest=v; return largest; } public static void main(String args[]) { System.out.println("Max:"+max(1,11,300,2,3,50,1500,2010,2019)); } }
The output is: 2019
Four point method overloading
Heavy Load relationship:
① the same method name
② different types of parameters, number of different parameters, a different order of parameters
Note: The method of the return value is not based on a judgment as
package com.java1; public class MethodOverload { public static void main(String[] args) { System.out.println("The square of integer 7 is " + square(7)); System.out.println("\nThe square of double 7.5 is " + square(7.5)); } public static int square(int x) { return x * x; } public static double square(double y) { return y * y; } }
The above code: There are two square method, but different parameter types, and therefore will choose different methods calculations are carried out at the time of call,
Output:
The square of integer 7 is 49
The square of double 7.5 is 56.25
Fifth, compare floating point numbers
In everyday computing will inevitably be used in the comparison of floating-point numbers, but in the comparison between the floating-point numbers can be directly used == it?
Answer: No
== can not be directly used to determine the value of two floating point numbers, but rather to use two numbers Absolute value of less than a very small number , the two numbers are equal.
package com.java1; public class CompareFloatNumber { /** * @param args */ public static void main(String[] args) { compare(); compare2(); } private static void compare() { double i = 0.0001; double j = 0.00010000000000000001; System.out.println(i==j); //输出:true } private static void compare2() { double i = 0.0001; double j = 0.000100000000600000001; if(Math.abs(i-j)<1e-20){ System.out.println("true"); } else { System.out.println("false"); } } }
Output:
true
false
Therefore, we can not compare to the same integer to floating-point comparison.