java随机数发生器

java随机数发生器,java,Java,我不明白滚动

我不明白滚动<1000的作用。我的意思是,我不明白为什么在rand函数生成随机数时使用它

public class Hello {
    public static void main(String[] args) {
        Random rand = new Random();
        int freq[] = new int[7];
        for (int roll = 1; roll < 1000; roll++) { // is there a reason for roll<1000
            ++freq[1 + rand.nextInt(6)];
        }
        System.out.println("Face \tFrequency");
        for (int face = 1; face < freq.length; face++) {
            System.out.println(face + "\t" + freq[face]);
        }
    }
}
公共类你好{
公共静态void main(字符串[]args){
Random rand=新的Random();
int freq[]=新的int[7];

对于(int roll=1;roll<1000;roll++){//roll是否有原因,因为他们最多只想生成999个随机数。

在这种情况下,roll被用作for循环中的计数器,一旦达到限制,就会中断循环。在本例中,限制为
1000
。由于roll被初始化为1,它将生成
999
个数字。

for(int roll=1;roll
roll
只是循环计数器。循环重复999次。这并不奇怪。它正在生成一个。您有一个六面骰子(
int freq[]=new int[7];
)并且您正在滚动999次(
int roll=1;roll我希望您不是看到该代码的公司的员工。否则,该公司将破产:)@jbabey是的!好吧,这看起来像一个骰子!我想知道为什么分配了7个整数,但只使用和打印了6个…这可能是为了匹配骰子上的数字,开始是1而不是0。你应该把它作为一个答案,这是确切的解释。从技术上讲,它打印了最后6个数字!我不确定为什么分配了7个整数,但o只使用了6个…是的,你是对的,我正在编辑它。编辑!很抱歉错过了。此外,此代码必须发布在codecrap.com上。。。
public class Hello {
    public static void main(String[] args) {
        Random rand = new Random();
        int freq[] = new int[7];
        for (int roll = 1; roll < 1000; roll++) { // is there a reason for roll<1000
            ++freq[1 + rand.nextInt(6)];
        }
        System.out.println("Face \tFrequency");
        for (int face = 1; face < freq.length; face++) {
            System.out.println(face + "\t" + freq[face]);
        }
    }
}
for (int roll =1; roll<1000;roll++){ // is there  a reason  for roll<1000
    ++freq[1+rand.nextInt(6)];
}
`public class Hello {
    public static void main(String[] args) {

        //Creates an instance of Random and allocates 
        //the faces of the dices on memory
        Random rand = new Random();
        int freq[] = new int[6];

        //Rolls the dice 1000 times to make a histogram
        for (int roll = 0; roll < 1000; roll++) {
            ++freq[rand.nextInt(6)];
        }

        //Prints the frequency that any face is shown
        System.out.println("Face \tFrequency");
        for (int face = 0; face < freq.length; face++) {
            System.out.println(face + "\t" + freq[face]);
        }
    }
}`