Yes.
Say we go over n lines in that algorithm.
The last line has a 1 in n chance (probablity we
want) of being selected.
If it isn't selected, then we use the line that was
previously selected, which was either the one before
which had a 1 in n-1 chance, correct for selecting from
the n-1 remaining values. If the n-1th line wasn't choosen
then the line before that had a correct 1 in n-2 chance
of being choosen for selecting from the n-2 remaining
values. And so on and so on, till you get to 1, which will
still be choosen if all others weren't.
I hope this is understandable.
update:
To attempt to clarify/summerize/whatever after seeing sierrathedog04's
response:
At any one point in the algorithm, the chance of that line
being choosen is correct for if the algorithm ended there,
and the chance for the line choosen before staying choosen
is correct
for if the algorithm ended there (1 out of n, and
n-1 out n, respectively.) So though earlier lines are
choosen with more liklihood at that point,
they may be overridden by the later choices
and everything turns out alright.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.