一、JDK中的Math类
java.lang.Math类提供了通用的数学函数方法,这些方法可以直接调用:Math.方法名(),也可以忽略类名只写静态方法名:方法名()
二、随机数生成
(1)使用random函数:产生的数的范围在 [最小值,最大值] -> 最小值+Math.random() *(最大值-最小值+1)
(2)使用Random类
1 import java.util.Arrays; 2 import java.util.Random; 3 4 public class Test1 5 { 6 public static void main(String[] args) 7 { 8 Random rand = new Random(); 9 System.out.println("rand.nextBoolean():" + rand.nextBoolean()); 10 byte[] buffer = new byte[16]; 11 rand.nextBytes(buffer); 12 System.out.println(Arrays.toString(buffer)); 13 //生成0.0~1.0之间的伪随机double数 14 System.out.println("rand.nextDouble():" + rand.nextDouble()); 15 //生成0.0~1.0之间的伪随机float数 16 System.out.println("rand.nextFloat():" + rand.nextFloat()); 17 //生成平均值是 0.0,标准差是 1.0的伪高斯数 18 System.out.println("rand.nextGaussian():" + rand.nextGaussian()); 19 //生成一个处于long整数取值范围的伪随机整数 20 System.out.println("rand.nextInt():" + rand.nextInt()); 21 //生成0~26之间的伪随机整数 22 System.out.println("rand.nextInt(26):" + rand.nextInt(26)); 23 //生成一个处于long整数取值范围的伪随机整数 24 System.out.println("rand.nextLong():" + rand.nextLong()); 25 } 26 }
相同“种子(seed)”的Random对象会生成相同的随机数。可以使用当前时间作为种子生成随机数。
1 import java.util.Random; 2 3 public class Test1 4 { 5 public static void main(String[] args) 6 { 7 Random r1 = new Random(50); 8 System.out.println("第一个种子为50的Random对象"); 9 System.out.println("r1.nextBoolean(): " + r1.nextBoolean()); 10 System.out.println("r1.nextInt(): " + r1.nextInt()); 11 System.out.println("r1.nextDouble(): " + r1.nextDouble()); 12 System.out.println("r1.nextGaussian(): " + r1.nextGaussian()); 13 System.out.println("---------------------------"); 14 15 Random r2 = new Random(50); 16 System.out.println("第二个种子为50的Random对象"); 17 System.out.println("r2.nextBoolean(): " + r2.nextBoolean()); 18 System.out.println("r2.nextInt(): " + r2.nextInt()); 19 System.out.println("r2.nextDouble(): " + r2.nextDouble()); 20 System.out.println("r2.nextGaussian(): " + r2.nextGaussian()); 21 System.out.println("---------------------------"); 22 23 Random r3 = new Random(100); 24 System.out.println("种子为100的Random对象"); 25 System.out.println("r3.nextBoolean(): " + r3.nextBoolean()); 26 System.out.println("r3.nextInt(): " + r3.nextInt()); 27 System.out.println("r3.nextDouble(): " + r3.nextDouble()); 28 System.out.println("r3.nextGaussian(): " + r3.nextGaussian()); 29 30 31 Random r4 = new Random(System.currentTimeMillis()); 32 System.out.println("以当前时间为种子的Random对象"); 33 System.out.println("r3.nextBoolean(): " + r4.nextBoolean()); 34 System.out.println("r3.nextInt(): " + r4.nextInt()); 35 System.out.println("r3.nextDouble(): " + r4.nextDouble()); 36 System.out.println("r3.nextGaussian(): " + r4.nextGaussian()); 37 } 38 }
(3)手写代码实现随机数生成
Seed:x0 a=16807 c=int.MaxValue C=0
要求:编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数。
1 import java.util.Scanner; 2 public class Test1 3 { 4 public static void RandomS(int n,int x) { 5 int m[]=new int[n+1]; 6 m[0]=x; 7 for(int i=1;i<=n;i++) { 8 m[i]=(m[i-1]*16807)%Integer.MAX_VALUE; 9 System.out.println(m[i]+" "); 10 } 11 } 12 public static void main(String[] args) 13 { 14 Scanner in=new Scanner(System.in); 15 System.out.print("请输入要生成随机数的数目:"); 16 int n=in.nextInt(); 17 System.out.print("请输入种子数:"); 18 int x=in.nextInt(); 19 RandomS(n,x); 20 } 21 }
三、参数可变的方法
1 public class Test1{ 2 public static double max(double...values) 3 { 4 double largest=Double.MIN_VALUE; 5 for (double v:values) 6 if(v>largest) largest=v; 7 return largest; 8 } 9 10 public static void main(String args[]) 11 { 12 13 System.out.println("Max:"+max(1,11,300,2,3)); 14 15 } 16 }
(1)只能出现在方法参数列表的最后
(2) “…”位于变量类型和变量名之间,前后有无空格均可
Java的方法重载
1 public class Test1 2 { 3 public static void main(String[] args) 4 { 5 System.out.println("The square of integer 7 is "+square(7)); 6 System.out.println("The square of integer 7 is "+square(7.5)); 7 } 8 public static int square(int x) { 9 return x*x; 10 } 11 public static double square(double y) { 12 return y*y; 13 } 14 }
四、满足重载的条件
(1)方法名相同;
(2)参数类型不同,参数个数不同,或者是参数类型的顺序不同。
注意:方法的返回值不作为方法重载的判断条件。