In single-threaded programs, you would typically generate (pseudo)random numbers using, and using a method such as nextDouble(). In performing the Monte Carlo simulation, each thread must compute many random samples. Note that the third column indicates the time needed to run approximately 1 billion samples. Running Monte Carlo simulation with n = 1073741824 samples. When I run the PiTester program on my computer, I get the following output (which takes a while to produce): For example, here is a skeleton of the PiEstimator class to get you started: The PiEstimator class must include a public instance method double getPiEstimate() that returns the desired estimate of \(\pi\). Be sure to use long for the number of samples, as this number could be larger than the maximum int value (which is a little over 2 billion). Specifically, your program must define a PiEstimator class that stores the number of samples and the number of threads used for its calculations. For example, if the number samples is 1000, and the number of threads is 10, each thread should perform 1000 / 10 = 100 samples (you may assume that the number of samples is evenly divisible by the number of threads). Your program should take two parameters-the number of samples, and the number of threads-and compute an estimate of \(\pi\) by performing the specified number of samples evenly distributed across the threads. Then \(\pi\) can be estimated from the proportion of samples that fall in the disk. ![]() Briefly, the idea of the procedure is to generate many random sample points (i.e., pairs of numbers) lying inside a square, and return the number of samples that lie inside a disk inscribed in the square. A conceptual description of such a procedure is described in the notes Monte Carlo Simulation. In this assignment, you will implement a multithreaded program that performs a Monte Carlo simulation to estimate the mathematical constant \(\pi \approx 3.14\ldots\). Estimating pi using a Monte Carlo simulation
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |