What does this program do? What is the program output if the user enters 10000 w
ID: 3663290 • Letter: W
Question
What does this program do?
What is the program output if the user enters 10000 when prompted for the number of points?
How would your answer to the above question change if the test in line 15 used <= rather than <?
Print the homework or copy and paste the code below in your homework. Draw a box around each statement, and underline each expression, in the code. (Even though technically an assignment in Java is an expression, do not underline entire assignments.)
The Random component provides a pseudo-random number generator that generates numbers uniformly distributed in the [0.0,1.0) interval; the method call rnd.nextDouble() returns a pseudo-random number in the [0.0,1.0) interval. (In case you're not familiar with the notation here, [0.0,1.0) denotes an interval consisting of all real numbers between 0.0 inclusive and 1.0 exclusive, i.e., the set of all values x satisfying 0.0 x < 1.0.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static void main(String[] args) {
SimpleReader input = new SimpleReader1L();
SimpleWriter output = new SimpleWriter1L();
output.print("Number of points: ");
int n = input.nextInteger();
int ptsInInterval = 0, ptsInSubinterval = 0;
Random rnd = new Random1L();
while (ptsInInterval < n) {
double x = rnd.nextDouble();
ptsInInterval++;
if (x < 0.5) {
ptsInSubinterval++;
}
}
double estimate = (100.0 * ptsInSubinterval) / ptsInInterval;
output.println("Estimate of percentage: " + estimate + "%");
input.close();
output.close();
}
https://www.dropbox.com/s/htxuzehj91prlbx/Homework_%20Monte%20Carlo%20Estimation.html?dl=0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
public static void main(String[] args) {
SimpleReader input = new SimpleReader1L();
SimpleWriter output = new SimpleWriter1L();
output.print("Number of points: ");
int n = input.nextInteger();
int ptsInInterval = 0, ptsInSubinterval = 0;
Random rnd = new Random1L();
while (ptsInInterval < n) {
double x = rnd.nextDouble();
ptsInInterval++;
if (x < 0.5) {
ptsInSubinterval++;
}
}
double estimate = (100.0 * ptsInSubinterval) / ptsInInterval;
output.println("Estimate of percentage: " + estimate + "%");
input.close();
output.close();
}
Explanation / Answer
The program is used to do the following:
Sample outputs for when user enters 10000 are as follows:
Output 1:
Number of points:
10000
Estimate of percentage: 49.45%
Output 2:
Number of points:
10000
Estimate of percentage: 49.24%
Output 3:
Number of points:
10000
Estimate of percentage: 50.14%
The outputs show that almost half the number of points are inserted into the sub-interval when user enters 10000 as the input.
If the test condition is changed from less-than (<) to less-than and equal to (<=), the probability of a point being inserted in the sub-interval increases by 1%.
Sample outputs for when user enters 10000 are as follows:
Output 1:
Number of points:
10000
Estimate of percentage: 50.13%
Output 2:
Number of points:
10000
Estimate of percentage: 50.19%
Output 3:
Number of points:
10000
Estimate of percentage: 49.47%
There is now a better chance of getting at least half of the total points into the sub-interval.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.