我的Math.random()方法总是返回1?为什么?

use*_*000 0 java

随机生成3个弹珠,如果它们相同,不同或者不同,我会比较它们.我的代码在下面,我的问题在上面......你可以帮帮我吗?

public static void marb(){
    int a[],b[];
    int num=0;

    a=new int[3];
    b=new int[3];

    a[0]=1;
    a[1]=2;
    a[2]=3;

    **num=(int)Math.random();** //num is always assigned  1

    for(int x=0;x<3;x++)
    {
        num=(int)Math.random();

        b[x]=a[num];

        System.out.println(""+x+". marble:"+b[x]);      

    }

    int x=0;

    if(b[x]==b[x+1] && b[x+1]==b[x+2])
        System.out.println("ALL SAME");
    else if(b[x]!=b[x+1] && b[x]!=b[x+2] && b[x+1]!=b[x+2])
        System.out.println("ALL DIFFERENT");
    else
        System.out.println("One is different");

    }
Run Code Online (Sandbox Code Playgroud)

sve*_*rre 12

Math.random()在和之间返回一个double.施法将始终截断为.01int0

如果要随机整数,请使用该Random.nextInt(range)方法.