此代码按预期打印"平均运行次数:0.99864197"
import java.util.Random;
public class A {
public static void main(String[] args) {
int min = -30;
int max = 1;
test(min, max);
}
static void test(int min, int max){
int count = 0;
Random rand = new Random(0);
for(int j = 0; j < 2097152; j++){
int number = min + rand.nextInt(max-min+1);
for(int i = 0; i < number; ++i) {
System.out.print("");
count++;
}
}
System.out.println("Average Number of Runs: " + count/65536F);
}
}
Run Code Online (Sandbox Code Playgroud)
此代码应打印相同的确切数字,但它会打印一个随机的负数.
import java.util.Random;
public class …Run Code Online (Sandbox Code Playgroud)