Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To estimate a definite integral with Monte Carlo in Python, sample points from the integration domain, evaluate the integrand at each point, then average those values and apply the domain-volume factor. The estimate is random—not an exact answer—and its uncertainty depends on the sampling method and the integrand.

How Monte Carlo integration estimates an integral

For a function integrated over a bounded box, draw independent, uniformly distributed points throughout the box. The integral is the box’s volume multiplied by the average of the function values at those points:

Î = V × mean(f(X)), where V = ∏(bᵢ − aᵢ) for bounds aᵢ ≤ xᵢ ≤ bᵢ.

This is the same idea as estimating an expectation. Uniform samples on a unit hypercube can be mapped to the desired box, with the volume factor accounting for the change in scale. For integration under a different sampling distribution, the estimator must include the appropriate density-based importance weight; the uniform-box formula cannot be reused unchanged.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Elebase USB to USB C Adapter for iPhone 18 Pro Max,USBC Car Charger Adapter
  • Read Before You Buy — No Video Output: These adapters support charging and USB 2.0 data transfer, but cannot transmit video signals. Except for standard USB webcams (which use USB data only), they are not compatible with HDMI/DisplayPort cables, video-capable USB-C hubs, or docking stations with video output.
  • Convert USB-A Ports to USB-C: Designed to connect USB-C earphones, cables, flash drives, card readers, and other USB-C accessories to standard USB-A ports. Plug-and-play with no drivers or software required.
  • Aluminum Alloy Housing: Built with a sturdy aluminum alloy shell that aids in heat dissipation and protects against daily wear and scratches. Designed to maintain a stable and secure connection.
  • Compact & Travel-Friendly: The ultra-compact design allows the adapter to stay plugged into your device without blocking adjacent ports or adding bulk, reducing wear and tear on your original USB ports.
  • 12-Month Warranty: Backed by a 12-month manufacturer warranty for peace of mind. Designed to meet strict quality control standards for reliable everyday performance.

A reproducible NumPy example

This example estimates the integral of x² + y² over the unit square. Since the square has volume 1, the estimate is just the mean of the sampled values. Its exact value, 2/3, is included as a teaching check, not as the result produced by a particular run.

import numpy as np

rng = np.random.default_rng(2026)
n = 200_000
points = rng.random((n, 2))
values = points[:, 0] ** 2 + points[:, 1] ** 2

estimate = values.mean()  # unit-square volume is 1
standard_error = values.std(ddof=1) / np.sqrt(n)
print(estimate, standard_error)

NumPy’s documented random-number workflow uses default_rng to create a Generator; the generator transforms a bit stream into samples from distributions. The seed 2026 makes the pseudorandom sequence repeatable in a compatible environment. See the NumPy random sampling documentation.

Rank #2
Anker USB-C Hub, 5-in-1 USB Hub for Laptops, 4K HDMI Multiport Adapter
  • 5-in-1 USB-C Hub: Experience comprehensive connectivity featuring a Power Delivery input, two USB-A 2.0 ports, a USB-A 3.0 port, and an HDMI port. (Note: The USB-C power delivery input port is only for connecting an external wall charger to power your laptop and cannot power peripheral devices.)
  • 90W Pass-Through Charging: Achieve optimal charging with 90W pass-through power to your laptop, supported by a total input of 100W, with the hub reserving 10W for operational efficiency. (Note: Wall charger not included.)
  • Quick Data Transfers: Accelerate your productivity with rapid data transfers using a high-speed 5Gbps USB 3.0 port and two 480Mbps USB 2.0 ports.
  • 4K HDMI Display: Enhance your visual experience with a hub capable of delivering 4K resolution at 30Hz in both mirror and extend modes. Please note that this hub is compatible with MacBook (macOS 12 and newer), Windows 10 and 11, ChromeOS, and laptops equipped with DP Alt Mode and Power Delivery. Note: This device is not compatible with Linux.
  • What You Get: Anker USB-C Hub (5-in-1, 4K HDMI), welcome guide, 18-month warranty, and our friendly customer service.

Adapting the example to a general box

For bounds [a₁, b₁] × … × [a_d, b_d], first generate unit-hypercube points, then scale each coordinate. Multiply the average function value by the product of the side lengths:

lower = np.array([a1, a2])
upper = np.array([b1, b2])
unit_points = rng.random((n, 2))
points = lower + (upper - lower) * unit_points
values = f(points)
volume = np.prod(upper - lower)
estimate = volume * values.mean()
standard_error = volume * values.std(ddof=1) / np.sqrt(n)

This sketch assumes f accepts an array of points and returns one value for each point. Vectorizing the integrand avoids a Python-level loop for each sample. For a domain in d dimensions, the point array has shape (n, d) in this manual NumPy approach.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
Anker USB C Hub, 7in1 Multi-Port USB Adapter, 4K@60Hz USBC to HDMI Splitter
  • Sleek 7-in-1 USB-C Hub: Features an HDMI port, two USB-A 3.0 ports, and a USB-C data port, each providing 5Gbps transfer speeds. It also includes a USB-C PD input port for charging up to 100W and dual SD and TF card slots, all in a compact design.
  • Flawless 4K@60Hz Video with HDMI: Delivers exceptional clarity and smoothness with its 4K@60Hz HDMI port, making it ideal for high-definition presentations and entertainment. (Note: Only the HDMI port supports video projection; the USB-C port is for data transfer only.)
  • Double Up on Efficiency: The two USB-A 3.0 ports and a USB-C port support a fast 5Gbps data rate, significantly boosting your transfer speeds and improving productivity.
  • Fast and Reliable 85W Charging: Offers high-capacity, speedy charging for laptops up to 85W, so you spend less time tethered to an outlet and more time being productive.
  • What You Get: Anker USB-C Hub (7-in-1), welcome guide, 18-month warranty, and our friendly customer service.

How to interpret the error estimate

For independent, identically distributed samples with finite variance, the standard error of the sample mean is the sample standard deviation divided by the square root of the sample count. Under uniform sampling over a box, multiply that standard error by the box volume, just as for the estimate itself.

The standard error describes sampling variability; it does not detect incorrect bounds, omitted regions, a mistaken model, or numerical problems inside the integrand. A fixed seed makes a run repeatable, but repeatability is not evidence that the estimate is accurate. Report the estimate, sample count, standard-error estimate, and seed; avoid digits that the uncertainty does not justify. Check stability by increasing the sample count or comparing independent runs.

Rank #4
Sale
UGREEN USB to USB C Adapter Combo 4-Pack, 10Gbps USB C Converter Space Gray
  • Dual Converters, Infinite Potential:Includes 2× USB C male to USB A female adapters and 2× USB A male to USB C female adapters. Perfect for a wide range of uses—tablets with Bluetooth keyboards, expand USB ports on macbook, and more. Two different converters for all your daily needs
  • Next-Level 10Gbps & 3A Charging: No more slow 480Mbps, this usb to usb c adapter has a transfer speed of up to 10Gbps, allowing you to do more transferring in less time. This usb adapter fits both USB A and USB C charger, supporting up to 3A fast charging
  • Upgraded Exquisite Craftsmanship: With an aluminum alloy housing and metal connector, the usbc to usb adapter is extremely durable and sturdy. Rigorously tested to withstand more than 10,000 times of plugging and unplugging, ensuring long-lasting performance
  • Broad Compatible: The usb c to usb adapter widely supports all USB C/ USB A devices like laptops, tablets, cellphones, car chargers, and phone chargers. Such as compatible with MacBook Pro/Air 2023/2022, Thunderbolt 4/3 Devices,Apple MagSafe Watch 9/8/7/SE/Ultra, iPad Pro 2022/2021, Samsung Galaxy S23/S20/S10, and iPhone 17/16/15 Pro. Plug and play
  • Please Note: To reach 10Gbps speed, keep the cable under 3.3 ft. For USB A Male to USB C adapters, try flipping the USB C connector. USB C Male to USB A adapters support bidirectional 10Gbps transfer within 3.3 ft

In SciPy’s stated Monte Carlo example, the error rate is described as O(n^-1/2), where n is the number of samples. This is a convergence-rate illustration, not a guarantee for every finite run or integrand. See SciPy’s Quasi-Monte Carlo tutorial.

Choosing between Monte Carlo, QMC, and quadrature

Method Sampling or structure Useful when Error information and cautions
Crude Monte Carlo Independent random points The integral is multidimensional, can be evaluated as a black box, or is naturally an expectation. With finite variance, typical error decreases like N^-1/2; individual runs vary.
Quasi-Monte Carlo (QMC) Structured low-discrepancy points, such as Sobol’ or Halton sequences The dimension and integrand suit low-discrepancy sampling; it can be especially useful for higher-dimensional integrals. Improvement is not assured for every function, and uncertainty is not interpreted exactly like IID standard error. Preserve sequence properties when generating points.
scipy.integrate.quad Adaptive QUADPACK-based one-dimensional quadrature An appropriate definite integral has one variable and adaptive quadrature is effective. Accepts absolute and relative tolerances and returns an estimated absolute error; inspect convergence information for difficult integrands.

The practical choice depends on dimension, integrand smoothness and behavior, cost per evaluation, whether you need incremental or reproducible sampling, and what kind of error information is useful. SciPy describes Monte Carlo methods as being used for optimization, numerical integration, and generating probability-distribution draws. Its QMC tutorial presents example-specific rates of O(n^-1/2) for Monte Carlo and O(n^-1) for a Sobol’ QMC example, noting that smoother functions can do better. Those examples are not universal rate guarantees.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Anker USB C Hub, 5-in-1 USBC to HDMI Splitter with 4K Display
  • 5-in-1 Connectivity: Equipped with a 4K HDMI port, a 5 Gbps USB-C data port, two 5 Gbps USB-A ports, and a USB C 100W PD-IN port. Note: The USB C 100W PD-IN port supports only charging and does not support data transfer devices such as headphones or speakers.
  • Powerful Pass-Through Charging: Supports up to 85W pass-through charging so you can power up your laptop while you use the hub. Note: Pass-through charging requires a charger (not included). Note: To achieve full power for iPad, we recommend using a 45W wall charger.
  • Transfer Files in Seconds: Move files to and from your laptop at speeds of up to 5 Gbps via the USB-C and USB-A data ports. Note: The USB C 5Gbps Data port does not support video output.
  • HD Display: Connect to the HDMI port to stream or mirror content to an external monitor in resolutions of up to 4K@30Hz. Note: The USB-C ports do not support video output.
  • What You Get: Anker 332 USB-C Hub (5-in-1), welcome guide, our worry-free 18-month warranty, and friendly customer service.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Using SciPy’s QMC tools

SciPy provides scipy.stats.qmc engines including Sobol’ and Halton, and qmc_quad for integration. The qmc_quad API documentation specifies that the integrand receives points shaped (d, n_points) and returns one value per point. This differs from the (n, d) layout used in the manual NumPy example, so adapt the integrand’s array handling accordingly.

qmc_quad performs multiple independently scrambled QMC estimates. Its returned estimate is the mean of those estimates; SciPy documents it as unbiased for the integral and says the standard error across estimates can be used with a Student t distribution with n_estimates − 1 degrees of freedom. Increasing n_points per estimate targets actual integration error, while increasing n_estimates improves the reported error estimate. These controls serve different purposes.

Preserve Sobol’ sequence structure

SciPy advises using a power-of-two number of points with Sobol’ sequences and warns against thinning the sequence or dropping its initial points. If an arbitrary point count is required, Halton may be an alternative, subject to the engine’s documented behavior. Consult the SciPy QMC guidance before changing sequence generation.

When to use quad instead

For a suitable one-dimensional definite integral, scipy.integrate.quad is often the more direct starting point than random sampling. It uses a QUADPACK-based adaptive method and lets you specify absolute and relative error tolerances. It returns both an integral estimate and an estimated absolute error; for challenging integrands, inspect the additional integration information rather than treating the error estimate as an unconditional guarantee. See the quad API documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Monte Carlo’s main practical advantage is that the same sampling idea extends naturally to many dimensions and black-box evaluations. That advantage does not make it the default for every integral: where one-dimensional adaptive quadrature applies, its tolerance controls and error output may be more useful.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.