compute and display the elapsed time for your computer to perform this same operations.using Java's "System.currentTimeMillis();" method to capture current time (in milli-seconds) before and after entering a loop or a method call, then subtract the earlier value from the later value to compute time difference. (code) //Fig. 7.7: RollDie.java //Die-rolling program using arrays instead of switch import java.security.SecureRandom; public class RollDie { public static void main(String[] args) { SecureRandom randomNumbers = new SecureRandom(); int[] frequency = new int[7]; // array of frequency counters // roll die 60,000,000 times; use die value as frequency index for (int roll = 1; roll <= 60_000_000; roll++) { ++frequency[1+ randomNumbers.nextInt(6)]; } System.out.printf("%s%10s%n", "Face", "Frequency"); //output each array element's value for (int face = 1; face < frequency.length; face++) { System.out.printf("%4d%10d%n", face, frequency[face]); } } }
compute and display the elapsed time for your computer to perform this same operations.using Java's "System.currentTimeMillis();" method to capture current time (in milli-seconds) before and after entering a loop or a method call, then subtract the earlier value from the later value to compute time difference.
(code)
//Fig. 7.7: RollDie.java
//Die-rolling program using arrays instead of switch
import java.security.SecureRandom;
public class RollDie {
public static void main(String[] args) {
SecureRandom randomNumbers = new SecureRandom();
int[] frequency = new int[7]; // array of frequency counters
// roll die 60,000,000 times; use die value as frequency index
for (int roll = 1; roll <= 60_000_000; roll++) {
++frequency[1+ randomNumbers.nextInt(6)];
}
System.out.printf("%s%10s%n", "Face", "Frequency");
//output each array element's value
for (int face = 1; face < frequency.length; face++) {
System.out.printf("%4d%10d%n", face, frequency[face]);
}
}
}
Trending now
This is a popular solution!
Step by step
Solved in 3 steps with 2 images