{
  "id": "monte-carlo-algorithm",
  "title": "Monte Carlo Algorithm",
  "lang": "en",
  "source_lang": "en",
  "summary": "A Monte Carlo algorithm uses randomness to achieve a bounded computational cost while allowing a controlled probability of an incorrect result.",
  "category": "computer-science",
  "subcategory": "algorithms-theory",
  "type": "concept",
  "aliases": [
    "Monte Carlo randomized algorithm"
  ],
  "generated_at": "2026-11-03",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/monte-carlo-algorithm/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/monte-carlo-algorithm/",
      "json": "https://aiwiki.page/zh/monte-carlo-algorithm.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "A Monte Carlo algorithm is a randomized algorithm that may return an incorrect result, but whose probability of error is bounded under stated assumptions. It typically guarantees a specified bound on running time, trading certainty of correctness for computational efficiency. Its guarantees concern each fixed input: the probability is taken over the algorithm’s internal random choices, not necessarily over a distribution of inputs. The term also has a broader numerical meaning, encompassing methods that estimate quantities through random sampling."
    },
    {
      "heading": "Definition and scope",
      "text": "An ordinary deterministic algorithm follows the same computational path whenever it receives the same input. A randomized algorithm additionally uses random choices, so its output, running time, or both may vary between executions.\n\nIn the standard decision-problem setting, a Monte Carlo algorithm \\(A\\) computes a Boolean function \\(f\\) with error bound \\(\\varepsilon\\) if, for every valid input \\(x\\),\n\n\\[\\Pr_r[A(x;r)\\ne f(x)]\\leq \\varepsilon,\\]\n\nwhere \\(r\\) denotes the internal random choices. Its running time is bounded by a stated function of input length, usually a polynomial in complexity-theoretic discussions. A conventional constant error bound is \\(1/3\\), although other constants below \\(1/2\\) can generally be reduced through repetition.\n\nThis is a worst-case-over-inputs, probabilistic-over-randomness guarantee. An algorithm that succeeds on most inputs but systematically fails on certain inputs does not satisfy it merely because its average success rate is high.\n\nIn numerical computation, an output is usually an estimate rather than a yes-or-no answer. The corresponding guarantee may instead take the form\n\n\\[\\Pr[|\\widehat{\\theta}-\\theta|>\\delta]\\leq \\alpha,\\]\n\nwhere \\(\\theta\\) is the target quantity, \\(\\widehat{\\theta}\\) is the estimate, \\(\\delta\\) is an error tolerance, and \\(\\alpha\\) is the permitted failure probability. Thus, “incorrect” can mean “outside the required tolerance,” rather than simply unequal to an exact value."
    },
    {
      "heading": "Relationship to other algorithmic approaches",
      "text": "Monte Carlo and Las Vegas algorithms\n\nA Las Vegas algorithm always returns a correct answer when it returns an answer, but its running time depends on random choices. Its performance is commonly described by an expected running-time bound.\n\nThe central distinction is therefore:\n\nApproach | Correctness | Running time\nMonte Carlo | Allows a bounded probability of error | Typically has a specified worst-case bound\nLas Vegas | Guarantees correctness of returned answers | Random; often analyzed in expectation\n\nThe names refer to different uses of randomness, not different application domains. Randomized quicksort, for example, is normally classified as Las Vegas: random pivot choices affect execution time, but not the correctness of the sorted output.\n\nA Las Vegas algorithm with a finite expected running time can sometimes be turned into a Monte Carlo algorithm by imposing a deadline and producing a fallback answer on timeout. The timeout probability then bounds the added error. Converting in the other direction requires additional structure, such as an efficient way to verify candidate answers; it is not universally possible.\n\nApproximation and randomization\n\nA Monte Carlo algorithm is not necessarily an approximation algorithm. It may seek an exact answer, such as whether an integer is prime, while allowing a small probability of answering incorrectly. Conversely, a deterministic approximation algorithm may always satisfy its approximation guarantee without using randomness.\n\nRandomized approximation methods combine the two ideas: an approximate answer must meet a specified accuracy requirement with a specified probability."
    },
    {
      "heading": "One-sided and two-sided error",
      "text": "Monte Carlo decision algorithms are commonly divided according to which answers can be wrong.\n\nOne-sided error means that one class of inputs is always handled correctly, while the other class may be misclassified. For example, a test may never call a prime number composite, yet occasionally classify a composite number as probably prime. The direction must be stated: “one-sided” alone does not identify which answer is conclusive.\n\nTwo-sided error means that either answer may be wrong, although the error probability is bounded separately for every input.\n\nThis distinction affects how repeated executions should be combined. For a one-sided test, one conclusive result may settle the decision. For a two-sided test, majority voting is a standard method of reducing error.\n\nIn computational complexity, the class BPP consists of decision problems solvable in polynomial time with bounded two-sided error. The class RP uses one-sided error: no-instances are always rejected, while yes-instances are accepted with probability at least \\(1/2\\). The complementary orientation gives co-RP."
    },
    {
      "heading": "Error reduction by repetition",
      "text": "Suppose each execution has error probability at most \\(p<1/2\\). If executions use independent random choices, running the algorithm \\(k\\) times and taking a majority vote reduces the error exponentially in \\(k\\).\n\nFor odd \\(k\\), a standard concentration bound gives\n\n\\[\\Pr[\\text{majority is wrong}] \\leq \\exp\\!\\left(-2k\\left(\\frac12-p\\right)^2\\right).\\]\n\nWhen \\(p\\) is bounded away from \\(1/2\\), reaching error probability at most \\(\\eta\\) therefore requires only \\(O(\\log(1/\\eta))\\) repetitions.\n\nFor a one-sided algorithm whose inconclusive or misleading outcome occurs with probability at most \\(p\\), requiring that outcome in all \\(k\\) independent repetitions yields an error bound of \\(p^k\\).\n\nIndependence is important. Repeating an execution with exactly the same random seed generally reproduces the same behavior rather than providing a new independent trial. Repetition also cannot repair a flawed mathematical model or a systematically incorrect implementation."
    },
    {
      "heading": "Representative examples",
      "text": "Miller–Rabin primality testing\n\nThe Miller–Rabin primality test is a standard example of a one-sided Monte Carlo algorithm. It tests algebraic conditions that every prime must satisfy. If a tested base violates those conditions, the input is certainly composite. If the conditions hold, the input remains a probable prime.\n\nFor an odd composite integer \\(n\\), at most one quarter of bases chosen uniformly from \\(1,\\ldots,n-1\\) pass a single strong probable-prime test. Consequently, \\(k\\) independent random rounds give a false probable-prime probability of at most\n\n\\[4^{-k}.\\]\n\nThis is a probability over the selected bases for a fixed composite input. It is not automatically the probability that a number is composite after passing the test; that latter interpretation also depends on how candidate numbers were selected.\n\nFor certain bounded input ranges, suitable predetermined bases give deterministic tests. Such variants should be distinguished from the randomized algorithm.\n\nPolynomial identity testing\n\nPolynomial identity testing asks whether an algebraically represented polynomial is identically zero. Expanding an expression into all its terms may be expensive, so an algorithm can instead evaluate it at randomly chosen points.\n\nThe Schwartz–Zippel lemma states that if a nonzero multivariate polynomial over a field has total degree at most \\(d\\), and each variable is sampled independently and uniformly from a finite subset \\(S\\) of that field, then\n\n\\[\\Pr[P(a_1,\\ldots,a_m)=0]\\leq \\frac{d}{|S|}.\\]\n\nAn identically zero polynomial evaluates to zero everywhere. A nonzero evaluation therefore proves nonidentity, whereas a zero evaluation may be misleading. Choosing a sufficiently large sampling set and repeating the test gives a controlled one-sided error probability.\n\nMatrix product verification\n\nFreivalds’ algorithm checks a proposed matrix product \\(AB=C\\) without directly computing every entry of \\(AB\\). For \\(n\\times n\\) matrices, it selects a random binary vector \\(r\\) and tests whether\n\n\\[A(Br)=Cr.\\]\n\nIf the product is correct, the equality always holds. If it is incorrect, a uniformly sampled binary vector fails to reveal the error with probability at most \\(1/2\\). A round uses \\(O(n^2)\\) arithmetic operations, compared with \\(O(n^3)\\) for straightforward matrix multiplication. Independent repetition reduces the probability of accepting an incorrect product."
    },
    {
      "heading": "Numerical Monte Carlo methods",
      "text": "The broader family of Monte Carlo methods uses random sampling to estimate integrals, probabilities, expected outcomes, and other quantities. The term became established through computational work associated with Stanisław Ulam, John von Neumann, and collaborators in the 1940s, particularly on particle transport.\n\nA basic example estimates the expected value \\(\\mu=\\mathbb{E}[X]\\) from independent samples:\n\n\\[\\widehat{\\mu}_N=\\frac{1}{N}\\sum_{i=1}^{N}X_i.\\]\n\nIf each sample has finite variance \\(\\sigma^2\\), then\n\n\\[\\mathbb{E}[\\widehat{\\mu}_N]=\\mu, \\qquad \\operatorname{Var}(\\widehat{\\mu}_N)=\\frac{\\sigma^2}{N}.\\]\n\nThe standard deviation of the estimator therefore decreases as \\(N^{-1/2}\\). Halving it ordinarily requires four times as many samples.\n\nThis sampling-rate exponent does not explicitly depend on dimension, helping explain Monte Carlo methods’ usefulness for high-dimensional integration. Nevertheless, variance, sample-generation cost, and the difficulty of representing rare events may depend strongly on dimension and problem structure.\n\nApplications include particle transport, financial simulation, statistical inference, and light-transport estimation in computer graphics. Monte Carlo tree search uses sampled outcomes to guide search decisions, but its name does not by itself imply a fixed finite-run error guarantee of the kind used in complexity theory."
    },
    {
      "heading": "Practical limitations and reliability",
      "text": "Randomness assumptions. Proofs commonly assume independent, uniformly distributed random bits or samples. Implementations generally use a pseudorandom number generator, whose finite state and deterministic behavior must be considered. In adversarial environments, predictability may matter as well as statistical quality.\n\nSampling bias and dependence. Samples drawn from the wrong distribution can invalidate an error guarantee. Correlation can reduce the effective amount of information obtained from repeated trials. Confidence calculations must reflect the actual sampling process.\n\nNumerical error. Mathematical guarantees may assume exact arithmetic. Rounding, overflow, or unstable computations introduce errors distinct from random sampling error.\n\nAccumulation across many decisions. A small per-execution failure probability is not automatically a small system-wide failure probability. If a system makes \\(m\\) decisions, each with error probability at most \\(\\varepsilon\\), the probability of at least one error is at most \\(m\\varepsilon\\) by the union bound, capped at 1. Independence is not required for this bound.\n\nVerification and failure consequences. Where outputs can be efficiently checked, verification can detect some failures or support a different algorithmic design. Where checking is difficult, reliability depends more directly on the validity of the probabilistic analysis and its implementation assumptions."
    }
  ],
  "links": [
    "randomized-algorithm",
    "algorithm",
    "quicksort",
    "approximation-algorithm",
    "computational-complexity",
    "miller-rabin-primality-test",
    "polynomial",
    "matrix",
    "monte-carlo-method",
    "expected-value",
    "variance",
    "integral",
    "computer-graphics",
    "monte-carlo-tree-search",
    "pseudorandom-number-generator"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}