This code calculates an estimate of the percentage of pseudo-random...

September 3, 2025 at 05:58 PM

import components.random.Random; import components.random.Random1L; import components.simplereader.SimpleReader; import components.simplereader.SimpleReader1L; import components.simplewriter.SimpleWriter; import components.simplewriter.SimpleWriter1L; /** * Monte Carlo Estimate: compute percentage of pseudo-random points in [0.0,1.0) * interval that fall in the left half subinterval [0.0,0.5). */ public final class MonteCarlo { /** * Private constructor so this utility class cannot be instantiated. */ private MonteCarlo() { } /** * Main method. * * @param args * the command line arguments; unused here */ public static void main(String[] args) { /* * Open input and output streams */ SimpleReader input = new SimpleReader1L(); SimpleWriter output = new SimpleWriter1L(); /* * Constant to convert a ratio into a percentage */ final double percentageFactor = 100.0; /* * Ask user for number of points to generate */ output.print("Number of points: "); int n = input.nextInteger(); /* * Declare counters and initialize them */ int ptsInInterval = 0, ptsInSubinterval = 0; /* * Create pseudo-random number generator */ Random rnd = new Random1L(); /* * Generate points and count how many fall in [0.0,0.5) interval */ while (ptsInInterval < n) { /* * Generate pseudo-random number in [0.0,1.0) interval */ double x = rnd.nextDouble(); /* * Increment total number of generated points */ ptsInInterval++; /* * Check if point is in [0.0,0.5) interval and increment counter if * it is */ if (x < 0.5) { ptsInSubinterval++; } } /* * Estimate percentage of points generated in [0.0,1.0) interval that * fall in the [0.0,0.5) subinterval */ double estimate = (percentageFactor * ptsInSubinterval) / ptsInInterval; output.println("Estimate of percentage: " + estimate + "%"); /* * Close input and output streams */ input.close(); output.close(); } }

This code calculates an estimate of the percentage of pseudo-random numbers generated in the interval [0.0, 1.0) that fall within the subinterval [0.0, 0.5) using the Monte Carlo simulation technique.

Steps of the Code:

  1. Input the Number of Points:

    • The user is prompted to enter the number of pseudo-random points to generate, denoted as n.
  2. Initialize Counters:

    • ptsInInterval: Tracks the total number of generated points.
    • ptsInSubinterval: Tracks how many of those points fall into the subinterval [0.0, 0.5).
  3. Generate Random Points:

    • A pseudo-random number generator (Random1L) is used to generate n random numbers in the range [0.0, 1.0).
    • For each random number:
      • The total count ptsInInterval is incremented.
      • If the number falls into the interval [0.0, 0.5), the count ptsInSubinterval is also incremented.
  4. Compute the Percentage:

    • Using the Monte Carlo estimate, the program calculates:
      estimate = (ptsInSubinterval / ptsInInterval) * 100
      
    • This converts the ratio of points in [0.0, 0.5) to [0.0, 1.0) into a percentage.
  5. Output the Estimate:

    • The estimated percentage is printed to the user.
  6. Close Streams:

    • Both input and output streams (SimpleReader and SimpleWriter) are closed.

Key Concepts:

  • Monte Carlo Simulation: A computational method that uses random sampling to estimate a numerical result.
  • Random Number Generation: Uses a pseudo-random generator to create numbers in the interval [0.0, 1.0).
  • Statistical Estimation: The ratio of points in [0.0, 0.5) out of all generated points serves as the estimate for the percentage.

Output Example:

If a user runs the program and provides:

  • Number of points as 10,000 (n = 10,000)

The program might output an approximate estimate close to 50%, since the subinterval [0.0, 0.5) is exactly half of [0.0, 1.0).

However, because the random points are generated pseudo-randomly, the result will fluctuate slightly around 50% due to the randomness inherent in the simulation.

Generate your own explanations
Download our vscode extension
Read other generated explanations

Built by @thebuilderjr
Sponsored by beam analytics
Read our terms and privacy policy
Forked from openai-quickstart-node