Java编程生成100个1~6之间的随机数,统计1~6每个数出现的概率;

实现了上述功能之后,之后修改程序使之生成1000个1~6之间的随机数并统计概率,比较不同的结果,并得出结论~!

    首先:关于随机数的生成方法

java里生成随机数主要有2个方法比较常用. 

1.Random类. 在 java.util.包里, 有多种方法可以随机产生int, long, double,boolean等类型

2.Math 类,在java.lang.包里, 可以生成一个[0~1)之间的随机浮点数

*备注* :a. )使用Random需要导包,但是使用Math无需导包. 

b.  )Math底层还是调用了Random类的nextDouble()方法

    其次:关于随机数的代码

例如生成[1,6]之间的随机数

1 .Random的方法,生成的随机数

Random r= new Random();
int num1 = r.nextInt(6)+1;//r.next(6)生成的是0~5之间的数字,需要+1,才是[1~6]之间的数字

2. Math的方法,进行随机数的生成

int num = (int) (Math.random() * 6) + 1;
//(int) (Math.random() * 6) 生成的是0~5之间的整数
//需要+1 才是[1~6]的数字

    关于次数统计的方案

    1, 可以使用HashMap<K,V>的方法进行存储统计. 因为key不重复,所以key可以来存数字1~6, 而对应的V就可以用来存储出现的次数

2. 可以使用数组的方法来存出现的次数. 数字1~6 是连续的, 数组下标也是连续的.我们可以用下标[0~5] 来代表数字[1~6], 数组的每个格子用来存数字数字出现的次数

    完整的代码

//本题使用数组来写代码比较方便,简洁.
import java.util.Random;

public class RandomDemo {
public static void main(String[] args) {
int[] times = new int[6];//6个格子的数组,存储出现的次数
int n = 100;//循环的次数
Random r= new Random();
for (int i = 0; i < n; i++) {
int num = r.nextInt(6)+1;//方法1 随机产生1~6的数字
//int num = (int) (Math.random() * 6) + 1;//方法2 随机产生1~6的数字
times[num - 1] = times[num - 1] + 1;//次数增加1
}

for (int i = 0; i < times.length; i++) {
System.out.println((i + 1) + "出现的次数" + times[i]);
}
}
}

输出:  当n=100时

1出现的次数13
2出现的次数16
3出现的次数17
4出现的次数23
5出现的次数21
6出现的次数10

输出: 当n=1000时

1出现的次数160
2出现的次数177
3出现的次数161
4出现的次数169
5出现的次数175
6出现的次数158

    结论:

    n=100时, 6是次数是10,  4次数是23,  两者的次数相差2倍多

    n=1000时, 6次数是158, 2次数是177, 两者比较接近

    说明:  当随机的次数越多. 那么随机数的概率也越来越接近~

温馨提示:答案为网友推荐,仅供参考
第1个回答  推荐于2018-02-27
import java.util.*;

public class TT {
public static void main(String[] args)
{
int[] a=new int[1000];
double[] b=new double[]{0.0,0.0,0.0,0.0,0.0,0.0};
Random t=new Random();
//for(int i=0;i<1000;i++)
for(int i=0;i<10;i++)
{
a[i]=t.nextInt(7);
System.out.println(a[i]);
if(a[i]==1)
b[0]++;
else if(a[i]==2)
b[1]++;
else if(a[i]==3)
b[2]++;
else if(a[i]==4)
b[3]++;
else if(a[i]==5)
b[4]++;
else
b[5]++;
}
System.out.println("1出现的概率是"+b[0]/1000);
System.out.println("2出现的概率是"+b[1]/1000);
System.out.println("3出现的概率是"+b[2]/1000);
System.out.println("4出现的概率是"+b[3]/1000);
System.out.println("5出现的概率是"+b[4]/1000);
System.out.println("6出现的概率是"+b[5]/1000);
}
}本回答被网友采纳
第2个回答  2013-06-02
上面的高手程序还是不错的,不过有个较大的错误:
a[i]=t.nextInt(7) ;应该改为a[i]=t.nextInt(5)+1;这样产生的随机数才在1-6之间.
第3个回答  2013-06-02
import java.util.HashMap;
import java.util.Random;

public class test {
private static Random r=new Random();
private static HashMap h=new HashMap();
public static int getNos(){return r.nextInt(6)+1;}
public static void add(int no){
if(h.containsKey(no))
h.put(no,(Object)((Integer.parseInt(h.get(no).toString())+1)));
else
h.put(no, 1);
}
public static void main(String[] args) {
for(int i=0;i<1000;i++)
add(getNos());
for(int c=1;c<7;c++)
System.out.println(c+"的出现频率是:"+Integer.parseInt(h.get(c).toString())/10.00+"%");
}
}
第4个回答  2013-06-02
import java.util.*;
class MyRandom{

public void randomTest(){
Random rand=new Random();
ArrayList a=new ArrayList();
ArrayList b=new ArrayList();
ArrayList c=new ArrayList();
ArrayList d=new ArrayList();
ArrayList e=new ArrayList();
ArrayList f=new ArrayList();
ArrayList g=new ArrayList();
for(int i=0;i<1000;i++){
int MyInt=rand.nextInt(6)+1;
if(MyInt==0){
a.add(i);
}
if(MyInt==1){
b.add(i);
}
if(MyInt==2){
c.add(i);
}
if(MyInt==3){
d.add(i);
}
if(MyInt==4){
e.add(i);
}
if(MyInt==5){
f.add(i);
}
if(MyInt==6){
g.add(i);
}
}
System.out.println("1的概率是:"+b.size()*0.1);
System.out.println("2的概率是:"+c.size()*0.1);
System.out.println("3的概率是:"+d.size()*0.1);
System.out.println("4的概率是:"+e.size()*0.1);
System.out.println("5的概率是:"+f.size()*0.1);
System.out.println("6的概率是:"+g.size()*0.1);
}
}
public class Test{
public static void main(String args[ ]) {
MyRandom ran=new MyRandom();
ran.randomTest();
}
}