动手动脑——方法

一、java已存在的随机数生成器

  要产生随机数,可以使用java.lang包中的Math类.Math类以静态方法的方式提供常用的数学方法,其中Math.random()方法是一个可以产生[0.0,1.0]区间内的一个双精度浮点数的方法,如:

产生一个1-50之间的随机数:int x=1+(int)(Math.random()*50)

  还可以使用java.util包中的Random类,如:

Random random1 = new Random();
System.out.println(random1.nextInt());    //生成整型随机数
System.out.println(random1.nextFloat());    //生成浮点型随机数
System.out.println(random1.nextBoolean());   //随机true或false
Random random2 = new Random(100);     //指定种子数100
System.out.println(random2.nextInt());
System.out.println(random2.nextFloat());
System.out.println(random2.nextBoolean());

二、根据公式自定义随机数生成器

  根据公式X_n+1 = (a X_n + c) mod m可以实现当显示过231-2个数之后,才可能重复的随机数生成器。以下为我根据公式写的生成随机数程序,用到了窗口的知识。

package random;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextArea;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;

public class RandomNumber extends JFrame implements ActionListener {
    public static int N = 1000;     //生成随机数的总数
    public static long Seed = 1;   //X0,且不能为0
    public static int a = 16807;  //Multiplier
    public static int c = 0;      //Increment
    public static int m = 2147483647;   //Modulos
    public static int min = 0;      //若Seed>0,则为生成的随机数范围下限;若Seed<0,则为随机数范围上限。
    public static int max = 2147483647;    //若Seed>0,则为生成的随机数范围上限;若Seed<0,则为随机数范围下限。
    //随机数种子发生变化
    public long produceSeed(long x) {
        return (a * x + c) % m;    //  X_n+1 = (a X_0 + c) mod m
    }
    //利用种子生成指定范围内的“随机”数
    public long produceRandom(long seed) {
        return (seed % ( max - min + 1 )) + min;
    }
    //递归调用
    public void recursion(long seed){
        if (N <= 0)
            return;
        Seed = produceSeed(seed);
        textArea.append("、" + produceRandom(Seed));
        N--;
        recursion(Seed);
    }
    //以下为窗口
    public JLabel formula, sumTotal, firstSeed, multiplier, increment, modulos, scope;
    public JButton change, produce, reset;
    public JTextArea textArea;
    public JScrollPane scrollPane;
    public RandomNumber(){
        setBounds(650,300,500,600);
        setTitle("生成随机数");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        init();
    }
    public void init() {
        setLayout(null);

        formula = new JLabel();
        formula.setText("公式:X_n+1 = (a X_0 + c) mod m");
        formula.setFont(new Font("",Font.BOLD,18));
        formula.setBounds(20,10,600,20);
        add(formula);
        
        sumTotal = new JLabel();
        sumTotal.setText("生成多少随机数:" + N);
        sumTotal.setFont(new Font("",Font.BOLD,18));
        sumTotal.setBounds(20,50,240,20);
        add(sumTotal);
        
        firstSeed = new JLabel();
        firstSeed.setText("初始种子(即X_0)为:" + Seed);
        firstSeed.setFont(new Font("",Font.BOLD,18));
        firstSeed.setBounds(250,50,190,20);
        add(firstSeed);

        multiplier = new JLabel();
        multiplier.setText("Multiplier(即a)为:" + a);
        multiplier.setFont(new Font("",Font.BOLD,18));
        multiplier.setBounds(20,70,240,20);
        add(multiplier);
        
        increment = new JLabel();
        increment.setText("Increment(即c)为:" + c);
        increment.setFont(new Font("",Font.BOLD,18));
        increment.setBounds(280,70,240,20);
        add(increment);
        
        modulos = new JLabel();
        modulos.setText("Modulos(即m)为:" + m);
        modulos.setFont(new Font("",Font.BOLD,18));
        modulos.setBounds(20,90,280,20);
        add(modulos);
        
        scope = new JLabel();
        scope.setText("生成的随机数范围为:" + min + "——" + max);
        scope.setFont(new Font("",Font.BOLD,18));
        scope.setBounds(20,110,480,20);
        add(scope);
        
        change = new JButton("更改条件");
        change.setBounds(30,140,140,30);
        change.setFont(new Font("",Font.BOLD,18));
        add(change);
        
        produce = new JButton("生成随机数");
        produce.setBounds(180,140,140,30);
        produce.setFont(new Font("",Font.BOLD,18));
        add(produce);
        
        reset = new JButton("清空所有");
        reset.setBounds(330,140,140,30);
        reset.setFont(new Font("",Font.BOLD,18));
        add(reset);
        
        textArea = new JTextArea();
        textArea.setFont(new Font("",Font.BOLD,15));
        textArea.setEditable(false);         //设置为只读
        textArea.setLineWrap(true);          //自动换行
        textArea.setBounds(20,190,450,370);
        add(textArea);
        
        scrollPane = new JScrollPane();
        scrollPane.setBounds(20,190,460,370);
        add(scrollPane);
        scrollPane.setViewportView(textArea);
        
        change.addActionListener(this);
        produce.addActionListener(this);
        reset.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == change) {
            try
            {
                N = Integer.parseInt(JOptionPane.showInputDialog("设置随机数总数为多少?(大于0的整数)"));
                sumTotal.setText("生成多少随机数:" + N);
                do{
                    Seed = Integer.parseInt(JOptionPane.showInputDialog("设置初始种子为多少?(不为0的整数)"));
                }while(Seed == 0);
                firstSeed.setText("初始种子(即X_0)为:" + Seed);
                a = Integer.parseInt(JOptionPane.showInputDialog("设置Multiplier为多少?"));
                multiplier.setText("Multiplier(即a)为:" + a);
                c = Integer.parseInt(JOptionPane.showInputDialog("设置Increment为多少?"));
                increment.setText("Increment(即c)为:" + c);
                m = Integer.parseInt(JOptionPane.showInputDialog("设置Modulos为多少?"));
                modulos.setText("Modulos(即m)为:" + m);
                min = Integer.parseInt(JOptionPane.showInputDialog("设置范围下限为多少?(种子为正,则下限大于0)"));
                max = Integer.parseInt(JOptionPane.showInputDialog("设置范围上限为多少?(种子为负,则上限小于0)"));
                if(min > max) {
                    int t = min;
                    min = max;
                    max = t;
                }
                scope.setText("生成的随机数范围为:" + min + "——" + max);
            }catch(NumberFormatException e1) {}
        }
        if (e.getSource() == produce) {
            int n = N;
            long seed = Seed; 
            recursion(Seed);
            textArea.replaceRange("", 0, 1);
            N = n;
            Seed = seed;
        }
        if( e.getSource() == reset ) {
            textArea.setText("");
        }
    }
    public static void main(String[] args) {
        new RandomNumber();
    }
}

三、方法重载

  满足以下条件的两个或多个方法构成“重载”关系: (1)方法名相同; (2)参数类型不同,参数个数不同,或者是参数类型的顺序不同。

  方法的返回值不作为方法重载的判断条件

  JDK中System.out.println()方法就用到了重载。

 public void println() {
        newLine();
    }
public void println(boolean x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }
public void println(char x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }
public void println(int x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }
public void println(long x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }
public void println(float x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }
public void println(double x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }
public void println(char x[]) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }
public void println(String x) {
        synchronized (this) {
            print(x);
            newLine();
        }
    }
public void println(Object x) {
        String s = String.valueOf(x);
        synchronized (this) {
            print(s);
            newLine();
        }
    }

 

posted @ 2018-10-14 17:15  星辰°  阅读(217)  评论(0编辑  收藏  举报