`
白粥若水
  • 浏览: 101252 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
社区版块
存档分类
最新评论

一个关于随机数类Random的小小游戏

阅读更多

 以前一直觉得Random类的方法一般是生成一个“随机”数,细想之下,这似乎与计算机的确定性相违背。后来在API文档上查阅才发现,此“随机”乃是“伪随机”,但到底是怎么个“伪”还是不清楚。今天做了一个小小程序,发现了一些有趣的事情

 

package 小游戏;
/**
 * 这是一个验证Random随机数的小游戏界面类
 */

import java.awt.FlowLayout;
import java.awt.Graphics;

import javax.swing.JButton;
import javax.swing.JFrame;

public class gameUI extends JFrame{
	
	
	public static void main(String args[]){
		gameUI gu = new gameUI();
		gu.init();
	} 
	
	public void init(){
		this.setTitle("小游戏");
		this.setSize(500, 500);
		
		FlowLayout fl = new FlowLayout();
		this.setLayout(fl);
		
		JButton jb =new JButton("开始随机数游戏");
		this.add(jb);
		
		JButton jbg = new JButton("开始高斯随机数游戏");
		this.add(jbg);
		
		this.setDefaultCloseOperation(3);
		this.setVisible(true);
		
		Graphics g = this.getGraphics();
		
		gameListener glis = new gameListener(g);
		jb.addActionListener(glis);
		
		gamegListener gglis = new gamegListener(g);
		jbg.addActionListener(gglis);
		
	}
}

 

package 小游戏;
/**
 * 小游戏的监听器类(生成一般随机数)
 */

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class gameListener implements ActionListener{
	
	private Graphics g;
	
	private int x1,x2,x3,y1,y2,y3;
	
	private int x,y;
	
	//重写监听器类的构造器方法
	public gameListener(Graphics g){
		this.g = g;
	}
	
	public void actionPerformed(ActionEvent e) {
		Random r = new Random();         //Random是一个生成伪随机数的类
		x1 = Math.abs(r.nextInt()%500);  //生成0到499之间的随机数(伪)
		x2 = Math.abs(r.nextInt()%500);
		x3 = Math.abs(r.nextInt()%500);
		y1 = Math.abs(r.nextInt()%500);
		y2 = Math.abs(r.nextInt()%500);
		y3 = Math.abs(r.nextInt()%500);
		System.out.println("x1:"+x1+"  x2:"+x2+"  x3:"+x3+"  y1:"+y1+"  y2:"+y2+"  y3:"+y3);
		
		x = Math.abs(r.nextInt()%500);
		y = Math.abs(r.nextInt()%500);
		
		
		g.drawLine(x1, y1, x1, y1);
		g.drawLine(x2, y2, x2, y2);
		g.drawLine(x3, y3, x3, y3);
		g.drawLine(x, y, x, y);
		
	
		for(int i =1;i<1000000;i++){
			int P = getP();
			getColor();
			
			//若随机数为1,则
			if(P==1){
				x = (x1+x)/2;
				y = (y1+y)/2;
				g.drawLine(x, y, x, y);
			}
			//若随机数为2,则
			else if(P==2){
				x = (x2+x)/2;
				y = (y2+y)/2;
				g.drawLine(x, y, x, y);
			}
			//若随机数为3,则
			else if(P==3){
				x = (x3+x)/2;
				y = (y3+y)/2;
				g.drawLine(x, y, x, y);
			}
		}
	}
	
	
	//随机取1,2,3中的一个数
	public int getP(){
		Random r = new Random();
		int P = Math.abs(r.nextInt()%3);
		return P+1;
	}
	
	
	public void getColor(){
		Random r = new Random();
		int P = Math.abs(r.nextInt()%10);
		int C = P+1;
		if(C==1){
			g.setColor(config.c1);
		}else if(C==2){
			g.setColor(config.c2);
		}else if(C==3){
			g.setColor(config.c3);
		}else if(C==4){
			g.setColor(config.c4);
		}else if(C==5){
			g.setColor(config.c5);
		}else if(C==6){
			g.setColor(config.c6);
		}else if(C==7){
			g.setColor(config.c7);
		}else if(C==8){
			g.setColor(config.c8);
		}else if(C==9){
			g.setColor(config.c9);
		}else if(C==10){
			g.setColor(config.c10);
		}else if(C==11){
			g.setColor(config.c11);
		}
	}
	
}

 

package 小游戏;
/**
 * 小游戏的监听器类(生成高斯随机数)
 */

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

public class gamegListener implements ActionListener{
	
	private Graphics g;
	
	private int x1,x2,x3,y1,y2,y3;
	
	private int x,y;
	
	//重写监听器类的构造器方法
	public gamegListener(Graphics g){
		this.g = g;
	}
	
	public void actionPerformed(ActionEvent e) {
		Random r = new Random();         //Random是一个生成伪随机数的类
		x1 = Math.abs(r.nextInt()%500);  //生成0到499之间的随机数(伪)
		x2 = Math.abs(r.nextInt()%500);
		x3 = Math.abs(r.nextInt()%500);
		y1 = Math.abs(r.nextInt()%500);
		y2 = Math.abs(r.nextInt()%500);
		y3 = Math.abs(r.nextInt()%500);
		System.out.println("x1:"+x1+"  x2:"+x2+"  x3:"+x3+"  y1:"+y1+"  y2:"+y2+"  y3:"+y3);
		
		x = Math.abs(r.nextInt()%500);
		y = Math.abs(r.nextInt()%500);
		
		
		g.drawLine(x1, y1, x1, y1);
		g.drawLine(x2, y2, x2, y2);
		g.drawLine(x3, y3, x3, y3);
		g.drawLine(x, y, x, y);
		
		
		
		for(int i =1;i<1000000;i++){
			int P = getP();
			
			//若随机数为1,则
			if(P==1){
				x = (x1+x)/2;
				y = (y1+y)/2;
				g.drawLine(x, y, x, y);
			}
			//若随机数为2,则
			else if(P==2){
				x = (x2+x)/2;
				y = (y2+y)/2;
				g.drawLine(x, y, x, y);
			}
			//若随机数为3,则
			else if(P==3){
				x = (x3+x)/2;
				y = (y3+y)/2;
				g.drawLine(x, y, x, y);
			}
		}
	}
	
	
	//随机取1,2,3中的一个数
	public int getP(){
		Random r = new Random();
		int P = Math.abs((int)r.nextGaussian()%3);
		return P+1;
	}

}

 

package 小游戏;

import java.awt.Color;

public class config {
	static Color c1 = Color.black;
	static Color c2 = Color.blue;
	static Color c3 = Color.cyan;
	static Color c4 = Color.gray;
	static Color c5 = Color.green;
	static Color c6 = Color.magenta;
	static Color c7 = Color.orange;
	static Color c8 = Color.pink;
	static Color c9 = Color.red;
	static Color c10 = Color.white;
	static Color c11 = Color.yellow;
}

 

 

     结果发现,生成的图像不是原来以为的不规则排列,而是一个在“数学上非常美丽的图形”。原来所谓的“随机”并不是完全的随机,在大尺度之下,也就是在统计学下,是有规律可找的

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics