java x = (int)(Math.random() * 1) 0 或 1 的概率是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13716106/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:44:19  来源:igfitidea点击:

x = (int)(Math.random() * 1) what are the chances of 0 or 1?

javamathrandom

提问by JRowan

that is the exact code then i have a switch for case 0: and case 1: it seems that the case 1: is coming out every time, i would like to have a 50/50 chance of 0 or 1 coming out is this the correct way or should i use 1.5 or how exactly does this work?

那是确切的代码然后我有一个切换案例 0: 和案例 1: 似乎案例 1: 每次都会出现,我希望有 50/50 的机会出现 0 或 1,这是正确的方法还是我应该使用 1.5 或者它到底是如何工作的?

talka = (int)(Math.random() * 1);
        switch(talka)
        {

        case 0:
        {
            talk.setAnimationListener(this);
            talk.playtimes(1,24);
            startService(new Intent(this, love1.class));
            break;
        }
        case 1:
        {
            talk.setAnimationListener(this);
            talk.playtimes(1,12);
            startService(new Intent(this, love2.class));
            break;
        }
        }

回答by Hovercraft Full Of Eels

Just use a java.util.Randomobject and simply call nextBoolean()on it which will return true or false in a 50:50 distribution. Easy as Math.PI.

只需使用一个java.util.Random对象并简单地调用nextBoolean()它,它将在 50:50 的分布中返回 true 或 false。简单如Math.PI

回答by Peter Lawrey

This always rounds down.

这总是向下取整。

talka = (int)(Math.random() * 1); // between 0 and 0

what you intended was perhaps

你想要的也许是

talka = (int)(Math.random() * 2); // between 0 and 1

However, using Math.random() get one bit is very inefficient.

然而,使用 Math.random() 得到一位是非常低效的。

If you use a Random with either

如果您使用随机

talka = random.nextInt(2);

or even better

甚至更好

talk.setAnimationListener(this);
if (random.nextBoolean()) {
        talk.playtimes(1,24);
        startService(new Intent(this, love1.class));
} else {
        talk.playtimes(1,12);
        startService(new Intent(this, love2.class));
}

回答by Lawrence Dol

The variable talkawill always be zero; Math.random returns a value where 0 <= x < 1; since x must be less than 1 and the (int)cast truncates the decimal component, the integer result will always be 0.

变量talka将始终为零;Math.random 返回一个值,其中 0 <= x < 1;由于 x 必须小于 1 并且(int)强制转换会截断小数部分,因此整数结果将始终为 0。

From the Math.random documentation:

从 Math.random 文档:

Returns a double value with a positive sign, greater than or equal to 0.0 and less than 1.0.

返回带正号的双精度值,大于或等于 0.0 且小于 1.0。

Use java.util.Random.nextBoolean()instead.

使用java.util.Random.nextBoolean()来代替。

回答by MadProgrammer

The problem has to do with the way the cast works.

问题与演员的工作方式有关。

In may test, Java was basically "trimming" the decimal result off and simply taking the "integer" component. However, if I rounded the result, I got it flipping between 0 and 1.

在可能的测试中,Java 基本上是“修剪”小数结果并简单地取“整数”组件。但是,如果我对结果进行四舍五入,它就会在 0 和 1 之间翻转。

Have a play

玩一玩

int ones = 0;
int zeros = 0;
for (int index = 0; index < 100; index++) {

    double rand = Math.random() * 1;
    if (Math.round(rand) == 1) {
        ones++;
    } else {
        zeros++;
    }
    System.out.println(rand + " - " + (int)Math.round(rand) + " - " + (int)Math.random() * 1);

}

System.out.println("Ones = " + ((float)ones / 100f));
System.out.println("Zeros = " + ((float)zeros / 100f));

It my simple test, I was getting around the 50/50 mark (+/-)

这是我的简单测试,我正在接近 50/50 标记 (+/-)

As pointed out by Hovercraft, better to use java.util.Randomin this case.

正如 Hovercraft 所指出的,java.util.Random在这种情况下最好使用。

回答by Kumar Vivek Mitra

-It would be better and easier to go with java.util.Random.

- 使用它会更好更容易java.util.Random

-Use the nextBoolean()method of its.

-使用其nextBoolean()方法。

Eg:

例如:

public class Rand {

    public static void main(String[] args){

        Random r = new Random();

        System.out.println(r.nextBoolean());  // See there is a equal
                                                      // true-false division
    }

}