Entropy — Measuring Uncertainty


slug: entropy-what-is-it-uncertainty-from-thermodynamics-to-information-theory-and-cryptography

title: "What Is Entropy — From Thermodynamics to Information Theory & Cryptography" lang: en excerpt: "What is entropy? From Boltzmann and Shannon (information theory) to cryptography (CSPRNG entropy pools) and machine learning (cross-entropy). Formulas, Python code, and practical applications." publishedat: "2026-04-04T07:48:11.002Z" updatedat: "2026-07-31T06:32:10.114Z"

Entropy — Measuring Uncertainty

1. Physical Entropy: Thermodynamics

The term 'entropy' (Ἐντροπία) was coined by Rudolf Clausius in 1865, derived from the Greek ἐν (en = in) + τροπή (tropē = transformation). In thermodynamics:

**Entropy () = Measure of disorder within a system**

The Second Law of Thermodynamics states that the total entropy of an isolated system always increases over time. Ice melting in warm water increases entropy. Ice never spontaneously re-freezes on its own without external work — entropy does not decrease spontaneously.

Boltzmann's entropy formula (carved onto his gravestone):

Where:

  • = Entropy
  • = Boltzmann constant ()
  • = Number of possible microstates
  • = Natural logarithm

2. Information Theory: Shannon Entropy

In his landmark 1948 paper A Mathematical Theory of Communication, Claude Shannon borrowed the term 'entropy' from physics to quantify the information content (or uncertainty) contained in a message.

Shannon Entropy:

Units: bits (when using ) or nats (when using ).

Calculation Examples:

import math

def shannon_entropyprobabilities:
    """Calculates Shannon entropy in bits."""
    return -sum(p * math.log2(p) for p in probabilities if p > 0)

# Fair coin toss: H = 1 bit
fair_coin = [0.5, 0.5]
print(f"Fair coin:       {shannon_entropy(fair_coin):.2f} bits")  # 1.00

# Biased coin (90% heads): H < 1 bit
biased = [0.9, 0.1]
print(f"Biased coin:     {shannon_entropy(biased):.2f} bits")  # 0.47

# Fair 6-sided die: H = log2(6) ≈ 2.585 bits
dice = [1/6] * 6
print(f"6-sided die:     {shannon_entropy(dice):.2f} bits")  # 2.58

# 1 uniformly random byte: H = 8 bits maximum
uniform_byte = [1/256] * 256
print(f"Random byte:     {shannon_entropy(uniform_byte):.2f} bits")  # 8.00

Intuitive Meaning:

  • : Completely deterministic — output is known with 100% certainty (no new information).
  • : Equivalent to one yes/no question — a fair coin toss.
  • : One completely random byte — requires 8 binary questions to determine.
  • : UUID v4 — possibilities (6 bits reserved for version/variant).
  • : Bitcoin/Ethereum private key — the universe will experience heat death long before brute-forcing is possible.

3. Cryptographic Entropy

In cryptography, entropy measures the unpredictable strength of randomness. A CSPRNG must gather sufficient physical entropy before generating secure cryptographic material.

3.1. OS Entropy Sources

  • /dev/random : Blocks execution if the entropy pool drains — historical maximum security (Linux < 5.6).
  • /dev/urandom : Non-blocking — seeds a CSPRNG once entropy is gathered; recommended for almost all general applications.
  • getrandom() system call (Linux 3.17+): Blocks only during boot if the initial pool lacks entropy — modern best practice.
  • CryptGenRandom / BCryptGenRandom : Gathers entropy across multiple hardware drivers and OS subsystems.
  • getentropy() (macOS / OpenBSD): Equivalent kernel interface to Linux getrandom().

3.2. Physical Entropy Sources

  • Hardware RNG: Intel RDRAND / RDSEED — measures thermal noise across semiconductor junction transistors.
  • Mouse / Keyboard timing: Measures microsecond jitter intervals during user input.
  • Disk I/O timing: Measures seek time variations and mechanical rotational latency.
  • Network Packet timing: Microsecond jitter during packet arrivals.
  • Quantum RNG: Measures photon polarization states — true physical randomness dictated by quantum mechanics.

Linux kernel entropy pool inspection:

# Check available entropy in Linux
cat /proc/sys/kernel/random/entropy_avail
# Output: 256 (bits available — if >= 256, getrandom() does not block)

# View pool size details
cat /proc/sys/kernel/random/poolsize
# Output: 256 (bits — fixed at 256 since Linux 5.18)

# Feed entropy from hardware RNG daemon
rngd -r /dev/hwrng  # rng-tools daemon

4. Password Entropy

Password entropy quantifies resistance against brute-force attacks in bits:

import math

def password_entropylength,charsets​ize:
    """Calculates password entropy in bits."""
    return length * math.log2charsets​ize

# Examples:
print(f"8 lowercase chars:        {password_entropy(8, 26):.1f} bits")   # 37.6
printf&quot;8 upper + lowercase:       {password_entropy(8, 52):.1f} bits&quot;   # 45.6
printf&quot;8 mixed + digits:          {password_entropy(8, 62):.1f} bits&quot;   # 47.6
print(f"8 all printable ASCII:     {password_entropy(8, 95):.1f} bits")   # 52.6
printf&quot;12 mixed + digits:         {password_entropy(12, 62):.1f} bits&quot;  # 71.5
print(f"4 English words (Diceware):{password_entropy(4, 7776):.1f} bits") # 51.7
print(f"6 English words (Diceware):{password_entropy(6, 7776):.1f} bits") # 77.5

# Recommended target: >= 80 bits for users, >= 128 bits for system keys

5. Applications in Information Theory & ML

Shannon entropy forms the theoretical foundation across multiple domains:

  • Data Compression: Entropy dictates the theoretical lower bound for lossless compression. A file cannot be losslessly compressed beyond its entropy limit. ZIP, GZIP, and Brotli approach Shannon limits.
  • Optimal Coding (Huffman / Arithmetic Coding): Assigns shorter variable-length codes to more frequent symbols.
  • Cross-Entropy Loss (Machine Learning): The standard loss function for classification models — quantifies divergence between predicted and true distributions:
  • Kullback-Leibler Divergence: Measures information gain/loss when approximating distribution using distribution :

6. Summary: The Four Dimensions of Entropy

  1. Physics: Measures macroscopic disorder — always increases in isolated systems (Second Law of Thermodynamics).
  2. Information Theory : Measures information content — minimum bits required to encode a message.
  3. Cryptography: Measures randomness strength — entropy bits backing keys, passwords, and seeds.
  4. Machine Learning: Cross-entropy — measures accuracy and prediction error between model output and ground truth.
Permissions
Distribution & Copying Modification & Remix Commercial use
Conditions
Attribution required ShareAlike required
Limitations
Third-party assets exempt No warranty