Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Java中的Math.random()生成概率相等的随机数_Java_Random_Numbers_Equals_Probability - Fatal编程技术网

如何使用Java中的Math.random()生成概率相等的随机数

如何使用Java中的Math.random()生成概率相等的随机数,java,random,numbers,equals,probability,Java,Random,Numbers,Equals,Probability,我目前正在做一个练习,要求我生成一个随机数,可以是4个值中的一个。(注意,我只允许使用Math.random()) 0 1. 2. 或3 目前我使用的是:randno=(int)(Math.random()*4);//范围0-3 然而,结果的概率必须相等。到目前为止,我的测试(尽管缺少该方法)显示3的出现率远远低于其他数字 这是巧合吗?或者我的生成器的概率不相等 谢谢 您的代码运行良好: public static void main(String[] args) { i

我目前正在做一个练习,要求我生成一个随机数,可以是4个值中的一个。(注意,我只允许使用Math.random())

0 1. 2. 或3

目前我使用的是:
randno=(int)(Math.random()*4);//范围0-3

然而,结果的概率必须相等。到目前为止,我的测试(尽管缺少该方法)显示3的出现率远远低于其他数字

这是巧合吗?或者我的生成器的概率不相等

谢谢

您的代码运行良好:

   public static void main(String[] args) {

        int countZero = 0;
        int countOne = 0;
        int countTwo = 0;
        int countThree = 0;

        for(int i=0; i<400000; i++){            
            int randno =  (int)(Math.random() * ((3) + 1));

            if(randno == 0){
                countZero++;
            }
            else if(randno == 1){
                countOne++;
            }
            else if(randno == 2){
                countTwo++;
            }
            else if(randno == 3){
                countThree++;
            }           
        }

        System.out.println("Zero: " + countZero);
        System.out.println("One: " + countOne);
        System.out.println("Two: " + countTwo);
        System.out.println("Three: " + countThree);


    }
您的代码运行良好:

   public static void main(String[] args) {

        int countZero = 0;
        int countOne = 0;
        int countTwo = 0;
        int countThree = 0;

        for(int i=0; i<400000; i++){            
            int randno =  (int)(Math.random() * ((3) + 1));

            if(randno == 0){
                countZero++;
            }
            else if(randno == 1){
                countOne++;
            }
            else if(randno == 2){
                countTwo++;
            }
            else if(randno == 3){
                countThree++;
            }           
        }

        System.out.println("Zero: " + countZero);
        System.out.println("One: " + countOne);
        System.out.println("Two: " + countTwo);
        System.out.println("Three: " + countThree);


    }

为了进行比较,我把你的方法放在一个旁边

公共类测试{
私有静态int min=0;
私有静态int max=3;
私有静态映射计数=新HashMap();
公共静态void main(字符串[]args){
OptionOne();
System.out.println(“选项一:”);
for(条目:count.entrySet()){
System.out.println(entry.getKey()+“\t”+entry.getValue());
}
count=新HashMap();
OptionTwo();
System.out.println(“\n选项二:”);
for(条目:count.entrySet()){
System.out.println(entry.getKey()+“\t”+entry.getValue());
}
}
专用静态无效OptionOne(){
对于(int i=0;i<800000;i++){
int number=min+(int)(Math.random()*((max-min)+1));
if(容器计数(数量)){
int sofar=count.get(number)+1;
计数。放置(数字,sofar);
}否则{
计数。放置(数字,1);
}
}
}
私有静态void OptionTwo(){
对于(int i=0;i<800000;i++){
整数=(int)(Math.random()*4);
if(容器计数(数量)){
int sofar=count.get(number)+1;
计数。放置(数字,sofar);
}否则{
计数。放置(数字,1);
}
}
}
输出:

Zero: 99683
One: 99793
Two: 100386
Three: 100138
选项一:
0 199853
1200118
2200136
199893

选项二:
0 199857
1200214
299488
3200441


结论:你的方法有效。也许你的样本量不够?

为了进行比较,我把你的方法放在了一个旁边

公共类测试{
私有静态int min=0;
私有静态int max=3;
私有静态映射计数=新HashMap();
公共静态void main(字符串[]args){
OptionOne();
System.out.println(“选项一:”);
for(条目:count.entrySet()){
System.out.println(entry.getKey()+“\t”+entry.getValue());
}
count=新HashMap();
OptionTwo();
System.out.println(“\n选项二:”);
for(条目:count.entrySet()){
System.out.println(entry.getKey()+“\t”+entry.getValue());
}
}
专用静态无效OptionOne(){
对于(int i=0;i<800000;i++){
int number=min+(int)(Math.random()*((max-min)+1));
if(容器计数(数量)){
int sofar=count.get(number)+1;
计数。放置(数字,sofar);
}否则{
计数。放置(数字,1);
}
}
}
私有静态void OptionTwo(){
对于(int i=0;i<800000;i++){
整数=(int)(Math.random()*4);
if(容器计数(数量)){
int sofar=count.get(number)+1;
计数。放置(数字,sofar);
}否则{
计数。放置(数字,1);
}
}
}
输出:

Zero: 99683
One: 99793
Two: 100386
Three: 100138
选项一:
0 199853
1200118
2200136
199893

选项二:
0 199857
1200214
299488
3200441


结论:你的方法有效。也许你的样本量不够?

使用随机数,你可以随机得到看似非随机的结果。不能保证所有序列都是随机的

对于正确的随机种子,您似乎可以得到一个非随机序列,但它们与其他任何序列一样可能

Random random = new Random(441287210);
for (int i = 0; i < 10; i++)
    System.out.print(random.nextInt(10)+" ");
}

最后

public static void main(String ... args) {
    System.out.println(randomString(-229985452)+' '+randomString(-147909649));
}

public static String randomString(int seed) {
    Random rand = new Random(seed);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; ; i++) {
        int n = rand.nextInt(27);
        if (n == 0) break;
        sb.append((char) ('`' + n));
    }
    return sb.toString();
}
印刷品

1 1 1 1 1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9
hello world

使用随机数,您可以随机获得看似非随机的内容。无法保证所有序列都是随机的

对于正确的随机种子,您似乎可以得到一个非随机序列,但它们与其他任何序列一样可能

Random random = new Random(441287210);
for (int i = 0; i < 10; i++)
    System.out.print(random.nextInt(10)+" ");
}

最后

public static void main(String ... args) {
    System.out.println(randomString(-229985452)+' '+randomString(-147909649));
}

public static String randomString(int seed) {
    Random rand = new Random(seed);
    StringBuilder sb = new StringBuilder();
    for (int i = 0; ; i++) {
        int n = rand.nextInt(27);
        if (n == 0) break;
        sb.append((char) ('`' + n));
    }
    return sb.toString();
}
印刷品

1 1 1 1 1 1 1 1 1 1
0 1 2 3 4 5 6 7 8 9
hello world

你做了多少次测试?应该是一样的。@felixfritz这个练习的测试方法不太好。它的工作原理是它指定了一个方向,3是向前的,2是向后的。我把它放在走廊里,它看起来向后走(生成了2),远比向前走(3)的多。但是这可能是巧合,所以我的方法是正确的?看@weston,这不是一个简单的问题,只是一个关于数字出现频率的问题。你做了多少次测试?应该是一样的。@felixfritz练习的测试方法不太好。它的工作方式是指定一个方向,3是向前的,2是向前的ng向后。我把它放在走廊里,它向后移动(生成了aka 2)的次数远远多于向前移动的次数(3)。但是这可能是巧合,所以我的方法是正确的?请看@weston它不是一个真正复杂的问题,这只是一个关于数字出现频率的问题。使用计数器数组可以大大减少这一点:)太棒了,只是想确定一下!谢谢你的帮助。@sje397你的意思是“array[randno]++”?@felixfritz是的,有一个循环来打印它们你可以用一系列计数器来减少很多:)太棒了,只是想确定一下!谢谢你的帮助。@sje397你是说用“array[randno]++”?@felixfritz是的,有一个循环来打印优先级