tut03ans - CS1020 Data Structures and Algorithms I Tutorial 3 Problem Solving with Java Library and Java Generics(For week 5 starting 10 February 2014

tut03ans - CS1020 Data Structures and Algorithms I Tutorial...

This preview shows page 1 - 3 out of 15 pages.

CS1020: Data Structures and Algorithms I 1 Tutorial 3 – Problem Solving with Java Library and Java Generics (For week 5, starting 10 February 2014) Suggested Solutions 1. [Java Random Numbers] Please refer to Lecture 2 slides 34-37 for this question . The Monte Carlo method uses repeated random sampling to iteratively arrive at a more and more accurate answer, and is used in many scientific experiments when it is difficult to arrive at an answer through pure calculation. Read Wikipedia for a quick guide to the “Monte Carlo” method. The Monte Carlo simulation can be used to find the approximate value of pi (π) . First, a circle of radius 𝑟 is drawn within a square. As the formula for the area of a circle is 𝜋𝑟 2 , the ratio of the area of the circle to the area of the square is given by: Area of Circle Area of Square = 𝜋𝑟 2 (2 𝑟 ) 2 = 𝜋𝑟 2 4 𝑟 2 = 𝜋 4 From above, it can be concluded that the ratio of the area of the circle to the area of the square is 𝜋 4 . Therefore, 𝜋 can be found by multiplying the ratio of the area of the circle to the area of the square by 4. The Monte Carlo simulation works by choosing multiple (x,y) coordinates from [-1.0, 1.0], and counting how many coordinates fall within the circle (this includes points that are exactly on the circumference of the circle). The circle is centered at (0, 0) and is bounded by a 2 units × 2 units square (see diagram on the right). a. The random numbers generated by the Random class are considered “pseudo-random” numbers. What does this mean? [Objectives] Know that the numbers generated are from an algorithm, and are not truly random Googling for the answer [Solution] Pseudo-random numbers are numbers that are generated from an algorithm to simulate random numbers. This is because computers are unable to generate true random numbers – to do so would require computers to act unpredictably, which is not what they are built to do. provides a good resource on pseudo- random numbers. 2 unit in length
Image of page 1