按公式产生随机数、java中的重载、递归、有关计算机计算的问题

1、按公式产生随机数
x1=(16807*x)%(Integer.MAX_VALUE)
x=x1;
通过这个公式进行随机数的产生,当产生的数字大于2e+32-2时,在用产生随机数的方式进行数字的输出。
主要思路是通过for训和和公式以及判断语句对产生的随机数进行判断以及输出。
package com.随机数;
import java.util.Random;
import java.util.Scanner;
public class Zhongzi 
{
    static Scanner sc=new Scanner(System.in);
    static public void Seed(long seed)
    {
        Random rand=new Random();
        long a;
        long b=0;
        long x1;
        System.out.println("输入想要产生的随机数的个数:");
        a=sc.nextLong();
        System.out.println("所有随机数字如下:");
        for(long i=1;i<=a;i++)
        {
          x1=(16807*seed)%(Integer.MAX_VALUE);//产生随机数的公式
          System.out.println(x1);
          b++;
          if(b<=((2e+32)-2))
             seed=x1;
          else
              seed=rand.nextLong();//当超过范围时可以用产生随机数的方法进行产生数字
        }
    }
    public static void main(String[] args) 
    {
        System.out.println(Integer.MAX_VALUE);
        long b;
        System.out.println("请输入想要输入的种子:");
        b=sc.nextLong();
        Seed(b);
    }

}

在学习随机数的时候看到了三中方法:

一个是Random r2 = new Random(50);类型。

一个是Random rand = new Random();类型

一个是Random r4 = new Random(System.currentTimeMillis());这种方法是最好的生成随机数的方法。

2、关于java中的函数重载

package java课堂2;
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;
    }
}

同样重要的是和C++一样函数的返回类型不能作为判断依据。

像这个例子是把参数的类型作为的判断依据。

3、关于递归函数

对于递归函数,主要是判断在递归的函数里是否有自己调用自己以及在函数开始时是否判断其是否满足相应的条件。

4、编译中的一些问题

由于计算机使用固定的位数来保存数值,因此,能处理的数值大小是有限的,当要处理的数值超过了这一范围时,计算机将会自动截断数值的二进制表示为它所能处理的最多位数,这将导致错误的处理结果。还有就是有关一个BigInteger类等,支持大整数等的加减乘除运算。

猜你喜欢

转载自www.cnblogs.com/dazhi151/p/11585868.html