我不明白roll <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]);
}
}
}
for (int roll =1; roll<1000;roll++){ // is there a reason for roll<1000
++freq[1+rand.nextInt(6)];
}
这里要做的是对频率数组上的随机位置加1 999次。
这里的"真正的"随机是rand.nextInt (6)生成一个介于0到6之间的数字。
:
for( int face=1;face<freq.length;face++){
System.out.println(face + "t"+freq[face]);
}
打印频率数组上的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]);
}
}
}`
现在它在内存上分配了6个int作为骰子的面,滚动1000次,很容易理解,因为它应该是
因为他们只想生成最多999个随机数
在这种情况下,roll被用作for循环中的计数器,一旦达到限制就中断循环。在这个例子中,极限是1000
。由于roll初始化为1,它将生成999
号