MCS-211 · Block 1 Unit 1

Basics of an Algorithm and its Properties

Section 9 of 9

Block 1 · Unit 1Basics of an Algorithm and its Properties10 min · Section 9 of 9

1.9Deterministic and Stochastic Algorithms

Covers IGNOU MCS-211 §1.8

Most algorithms you have met so far behave the same way every time on the same input. Some algorithms deliberately use random choices. This page explains the difference, when randomness helps, and where random numbers come from.

The two kinds

Randomized algorithms come in two main types:

TypeOutputRunning timeExample
Las VegasAlways correctRandom (good on average)Randomized quicksort
Monte CarloCorrect with high probabilityFixed boundMiller–Rabin primality test
Python · random_quicksort.py
import random

comparisons = 0

def quicksort(a):
    """Randomised quicksort: the pivot is chosen at random."""
    global comparisons
    if len(a) <= 1:
        return a
    pivot = random.choice(a)
    smaller, equal, larger = [], [], []
    for x in a:
        comparisons += 1
        if x < pivot:
            smaller.append(x)
        elif x > pivot:
            larger.append(x)
        else:
            equal.append(x)
    return quicksort(smaller) + equal + quicksort(larger)

data = [38, 27, 43, 3, 9, 82, 10, 55]
for run in range(1, 4):
    comparisons = 0
    print(f"run {run}: {quicksort(data)} comparisons = {comparisons}")
Output (the comparison counts differ on every run)
run 1: [3, 9, 10, 27, 38, 43, 55, 82] comparisons = 21
run 2: [3, 9, 10, 27, 38, 43, 55, 82] comparisons = 26
run 3: [3, 9, 10, 27, 38, 43, 55, 82] comparisons = 22

The sorted output is identical every time; only the amount of work changes. That is a Las Vegas algorithm: random steps, predictable (correct) result.

Why use randomness?

  • No bad inputs. Deterministic quicksort that always picks the first element as pivot takes Θ(n2)\Theta(n^2) time on already-sorted input. A random pivot makes every input equally safe: the expected time is O(nlog⁡n)O(n \log n) for every input.
  • Speed and simplicity. Monte Carlo primality tests check huge numbers far faster than known deterministic methods of similar simplicity.
  • Unpredictability is the goal. Card shuffling in an online game, generating passwords or encryption keys: here the output must not be predictable, and a deterministic method cannot provide that.

Where random numbers come from

  • A pseudorandom number generator (PRNG) is itself a deterministic algorithm: from a starting value called the seed, it produces numbers that look random. If someone learns the seed or the internal state, they can predict every future number. Fine for simulations and games of chance with no money at stake; unsafe for security.
  • A cryptographically secure PRNG (CSPRNG), seeded with unpredictable data, is designed so that its outputs cannot be predicted in practice. Use it for passwords, tokens and shuffling with real stakes (for example Python’s secrets module, not random).
  • A hardware random number generator measures physical noise (thermal noise, timing jitter). Modern operating systems mix such hardware entropy into their CSPRNG.

Key takeaways

  • Deterministic: same input → same steps and output, every time.
  • Stochastic (randomized): uses random choices; steps, time or output may vary between runs.
  • Las Vegas: always correct, random time; Monte Carlo: fixed time, small chance of error.
  • PRNGs are deterministic and predictable; security needs a CSPRNG or hardware randomness.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

Randomized quicksort is run three times on the same list. Which statement is true?

Show answer

Answer: B. It always returns the same sorted list, but the number of comparisons may differ.

It is a Las Vegas algorithm: the output is always correct and therefore identical; only the work varies. Monte Carlo algorithms may give a wrong answer.

Question 2 · multiple choice

Which algorithm is deterministic?

Show answer

Answer: A. Euclid’s GCD algorithm

Euclid’s algorithm uses only the input to decide every step. The other three use random choices.

Question 3 · multiple choice

Which source should be used to generate passwords?

Show answer

Answer: C. A cryptographically secure PRNG such as Python’s secrets

Passwords must be unpredictable; a CSPRNG is designed for this. A fixed seed or the date makes the output easy to reproduce.

Question 4 · multiple choice

A Monte Carlo algorithm:

Show answer

Answer: D. has a bounded running time but may be wrong with small probability

That is the definition. “Always correct, random time” describes Las Vegas algorithms.

Question 5 · multiple choice

Quicksort that always picks the first element as pivot is given an already sorted array of nn distinct elements. Its running time is:

Show answer

Answer: B. Θ(n2)\Theta(n^2)

Each pivot is the smallest element, so one side is empty and the other has n − 1 elements; the work is about n + (n−1) + … = Θ(n²). A random pivot avoids this, giving expected Θ(n log n).

Question 6 · short answer

Differentiate deterministic and stochastic algorithms with one example of each.

Show worked answer

A deterministic algorithm performs the same steps and gives the same output every time it is run on the same input; example: Euclid’s GCD algorithm or linear search. A stochastic (randomized) algorithm uses random numbers during execution, so its steps, running time or output may differ between runs on the same input; example: randomized quicksort (always correct, running time varies — Las Vegas) or the Miller–Rabin primality test (fast, small chance of error — Monte Carlo). Stochastic algorithms are needed when the output must be unpredictable (card shuffling, key generation) and are useful to avoid bad worst-case inputs.

↑ ↓ move↵ openTab switch type