TeachMeBitcoin

The Random Draw: Simple Random Selection for Privacy and Speed

From TeachMeBitcoin, the free encyclopedia Reading time: 4 min

11. The Random Draw: Simple Random Selection for Privacy and Speed

In the world of "High-Precision Finance," sometimes "Simple" is better than "Complex." While the BnB and Knapsack algorithms are powerful, they are also "Predictable." If a chain analysis company knows exactly how the Bitcoin Core "Knapsack" works, they can use that knowledge to "Identify" your wallet based on its selection patterns. To combat this, Bitcoin Core includes a "Privacy-First" fallback called Single Random Draw (SRD). SRD is the simplest algorithm in the toolkit: it shuffles your inventory and picks coins at random until it has enough to pay the target. It is the "Roll of the Dice" in the forge.

SRD is used as a "Tie-Breaker" or a "Fallback" when the other algorithms fail or are too slow. But for the Sovereign Architect, SRD is a vital part of the "Privacy Shield." By introducing "True Randomness" into the selection process, it breaks the "Deterministic Patterns" that spies use to track you. It is the "Unpredictability of the Sovereign."

Analyzing the Dice Roll: SelectCoinsSRD

In the source code (src/wallet/coinselection.cpp), we can see the "Simplicity" of this logic. It is a "Linear Scan" of a randomized list. It is the fastest algorithm in the wallet.

/**
 * This is the Single Random Draw algorithm. It picks coins at random until the target is met.
 */
util::Result<SelectionResult> SelectCoinsSRD(const std::vector<OutputGroup>& utxo_pool, ...)
{
    // 1. We "Shuffle" the entire inventory of coins.
    std::vector<size_t> indexes(utxo_pool.size());
    std::iota(indexes.begin(), indexes.end(), 0);
    std::shuffle(indexes.begin(), indexes.end(), rng);

    // 2. We walk through the shuffled list and pick coins one by one.
    CAmount selected_value = 0;
    for (const size_t i : indexes) {
        const OutputGroup& group = utxo_pool.at(i);

        // 3. We add the coin to the current selection.
        result.AddInput(group);
        selected_value += group.GetSelectionAmount();

        // 4. We stop as soon as the total in our hand is "Enough" to cover the bill.
        if (selected_value >= target_value) {
            return result;
        }
    }
}

Explaining the Random Draw: The Blind Bag

The Privacy of the "Unoptimized"

Why would you ever want to use a "Simple" algorithm when you have "Sophisticated" ones? Because Optimization is a Fingerprint. A wallet that always picks the "Perfect Fit" is easy to spot. A wallet that picks "Randomly" looks like "Any Other Wallet." For the Sovereign Architect, "Blending In" is a key part of your security. SRD is the "Camouflage" of the ledger. It ensures that your bank's behavior doesn't stand out in the global sea of transactions. You are the "Master of the Mundane," protecting your wealth through the power of the "Average."


☕ Help support TeachMeBitcoin

TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!