以前一直觉得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;
}
结果发现,生成的图像不是原来以为的不规则排列,而是一个在“数学上非常美丽的图形”。原来所谓的“随机”并不是完全的随机,在大尺度之下,也就是在统计学下,是有规律可找的
分享到:
相关推荐
文章主要介绍shell脚本如何获取随机数random的方法,感兴趣的朋友可以看看。
Python random 生成随机数的几个常用 函数
本程序利用C++产生随机数,每次产生的随机数都不一样
此文档以一个小的实例讲解了java随机数random的使用,在java当中随机数的使用率较高,所以需要熟练掌握随机数的使用
随机数随机数随机数随机数随机数随机数随机数随机数
//产生随机数的类 final SuiJiShuClass sjs=new SuiJiShuClass(); final JPanel panel=new JPanel(); //初始化一个JPanel panel.setBorder(BorderFactory.createTitledBorder("QQ:330936274")); //设置边界 ...
STM32 Random 产生随机数 在通讯中加密防止破解
自定义随机数,再添加数据是往往想要自己定义一个id好而且还让她没有规律那么这时候就需要随机数了这个例子包括了所有Random 的例子!
随机数生成类随机数生成类随机数生成类随机数生成类随机数生成类
就是Random类的简单随机数产生···就是这样,给需要的人!
今天有朋友向我请教:...random() 方法可返回介于 0 ~ 1 之间的一个随机数。 语法 Math.random() 返回值 0.0 ~ 1.0 之间的一个伪随机数。 实例 在本例中,我们将取得介于 0 到 1 之间的一个随机数: <script typ
一个生成随机数的小工具
c++编写的随机数类 产生随机数需要 随机种子 当种子为0时则用系统时间产生随机种子 并用随机数实现了抛硬币的应用
一个产生随机数的操作类,可自由定制随机数的类型个数等 用于产生校验码等
零基础Python教程034期_random随机数开发猜数字游戏#编程创造城市
c++ 可以输入条件的VC++随机数生成程序 random
随机数类、图片、文件操作类、压缩解压缩、验证码 类源码集合
C#写的随机数生成工具,有各种随机取值的方法
一个猜数字游戏实例,有一些注释。猜数字游戏,输入一个数字与随机数比较。实现简单,适合初学者借鉴。