The Random Draw: Simple Random Selection for Privacy and Speed
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
-
std::iotaandstd::shuffle: Imagine you have 100 coins. You put a "Number Tag" (0 to 99) on each one. Then you put all the numbers into a "Bag" and shake it vigorously. You then pull out numbers one by one. This ensures that the "Order" in which you look at your coins has "Zero Relationship" to their value, their age, or their address. It is the "Purest Form of Chance," protecting your privacy through the "Chaos of the Deck." -
selected_value >= target_value: This is the "Brute Force" of the payment. We don't care about "Exact Change" (BnB) or "Optimal Approximations" (Knapsack). We just want to "Get the Job Done." As soon as the total in our hand is enough to cover the bill, we stop. This is incredibly efficient for the computer's CPU, making it the perfect fallback for slow devices. It is the "Speed of the Sovereign." -
target_value += CHANGE_LOWER: Wait, if SRD is "Simple," why does it add a "Change Lower" buffer? Because if we stopped exactly at the target, we might create a "Tiny Change Output" (Dust) that is worth less than the fee needed to spend it. By adding a small buffer, SRD ensures that any change we create is "Meaningful" and "Spendable." It is the "Prudence of the Random."
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."
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: