Everyone here did a great job of explaining how the code works and showing how you can construct your own examples, but here's an information theoretical answer showing why we can reasonably expect a solution to exist that the brute force search will eventually find.
The 26 different lower-case letters form our alphabet Σ
. To allow generating words of different lengths, we further add a terminator symbol ⊥
to yield an extended alphabet Σ' := Σ ∪ {⊥}
.
Let α
be a symbol and X a uniformly distributed random variable over Σ'
. The probability of obtaining that symbol, P(X = α)
, and its information content, I(α)
, are given by:
P(X = α) = 1/|Σ'| = 1/27
I(α) = -log₂[P(X = α)] = -log₂(1/27) = log₂(27)
For a word ω ∈ Σ*
and its ⊥-
terminated counterpart ω' := ω · ⊥ ∈ (Σ')*
, we have
I(ω) := I(ω') = |ω'| * log₂(27) = (|ω| + 1) * log₂(27)
Since the Pseudorandom Number Generator (PRNG) is initialized with a 32-bit seed, we can expect most words of length up to
λ = floor[32/log₂(27)] - 1 = 5
to be generated by at least one seed. Even if we were to search for a 6-character word, we would still be successful about 41.06% of the time. Not too shabby.
For 7 letters we're looking at closer to 1.52%, but I hadn't realized that before giving it a try:
#include <iostream>
#include <random>
int main()
{
std::mt19937 rng(631647094);
std::uniform_int_distribution<char> dist('a', 'z' + 1);
char alpha;
while ((alpha = dist(rng)) != 'z' + 1)
{
std::cout << alpha;
}
}
See the output: http://ideone.com/JRGb3l
n
infor (int n = 0; ; n++)
. They could usefor(;;)
orwhile(true)
instead! – Eng.FouadfixedAndNotSoRandomString
or something... – MC Emperor