41
3
Given an input string S, print S followed by a non-empty separator in the following way:
Step 1:
Shas a1/2chance of being printed, and a1/2chance for the program to terminate.Step 2:
Shas a2/3chance of being printed, and a1/3chance for the program to terminate.Step 3:
Shas a3/4chance of being printed, and a1/4chance for the program to terminate.…
Step
n:Shas an/(n+1)chance of being printed, and a1/(n+1)chance for the program to terminate.
Notes
The input string will only consist of characters that are acceptable in your language's string type.
Any non-empty separator can be used, as long as it is always the same. It is expected that the separator is printed after the last print of
Sbefore the program terminates.The program has a
1/2chance of terminating before printing anything.A trailing new line is acceptable.
Your answer must make a genuine attempt at respecting the probabilities described. Obviously, when
nis big this will be less and less true. A proper explanation of how probabilities are computed in your answer (and why they respect the specs, disregarding pseudo-randomness and big numbers problems) is sufficient.
Scoring
This is code-golf, so the shortest answer in bytes wins.
Can the separator be an empty string? – rturnbull – 2017-06-12T07:13:54.133
16@rturnbull Well no, because in that case there is no separator. – Fatalize – 2017-06-12T07:18:29.167
Do we have to print these one after the other, or can we just print all of them when the program terminates? – Dennis – 2017-06-12T18:21:34.760
@Dennis One after the other. – Fatalize – 2017-06-13T06:42:46.447