This will eventually stop…
C#, 94 85 bytes
My first answer!
using System;s=>{var r=new Random();for(var i=2;r.Next(i++)>0;)Console.Write(s+" ");}
Previous attempt (I liked that goto
):
using System;s=>{var i=2;var r=new Random();a:if(r.Next(i++)>0){Console.Write(s+" ");goto a;}}
Ungolfed:
using System;
class P
{
static void Main()
{
Action<string> f = s =>
{
var r = new Random();
for (var i = 2; r.Next(i++) > 0;) Console.Write(s + " ");
};
f("test");
Console.ReadKey();
}
}
Note: in C# the Random.Next(N)
method returns a nonnegative integer in the [0, N-1] range, so we can just check that the number returned is greater than 0.
Pyth, 7 bytes
WOh=hZQ
Try it online!
How it works
Pseudocode:
while rand_int_below(1 + (Z += 1)):
print(input)
R, 47 46 43 bytes
43 bytes due to Robin Ryder in the comments.
s=scan(,"")
while(sample(T<-T+1)-1)print(s)
Try it online!
Explanation
s=scan(,"") # Takes input from stdin.
T<-T+1 # T is 1 by default, so this
# evaluates to 2, and will increment
# at each step.
sample(T<-T+1) # Take a sample of size 2, i.e. generate
# a list of integers from 1 to 2 in random order
sample(T<-T+1)-1 # Subtract one from every element of this list.
while(sample(T<-T+1)-1)# while() will treat the first value in this list
# as a logical value, i.e. FALSE for zero and TRUE
# for nonzero values. The other elements of the list
# are ignored, triggering a warning.
print(s) # print s