| Bytes | Lang | Time | Link |
|---|---|---|---|
| 060 | AWK | 250424T151307Z | xrs |
| 092 | Nim | 250328T030511Z | janAkali |
| 011 | Vyxal | 230810T202154Z | Joao-3 |
| 011 | Thunno 2 | 230810T123930Z | The Thon |
| 011 | Japt | 180112T165653Z | Shaggy |
| 113 | Emacs Lisp | 190420T001533Z | adl |
| 052 | Perl 6 | 190419T222506Z | bb94 |
| 029 | J | 190416T043722Z | Jonah |
| 012 | Charcoal | 171106T005330Z | Neil |
| 073 | PowerShell | 190416T153618Z | Veskah |
| 075 | R | 171106T195106Z | Giuseppe |
| 017 | Brachylog | 190415T212836Z | Unrelate |
| 024 | Noether | 180730T183156Z | Beta Dec |
| 091 | JavaScript | 180118T103849Z | david |
| 017 | Jelly | 180118T050009Z | dylnan |
| 091 | 05AB1E | 180112T170842Z | Magic Oc |
| 018 | Pushy | 171108T183627Z | FlipTack |
| 192 | C | 171105T224651Z | Steadybo |
| 053 | Julia 0.6 | 180116T234837Z | gggg |
| 074 | Javascript | 171108T141907Z | RuteNL |
| nan | GNU sed + coreutils | 171106T084558Z | seshouma |
| 026 | Japt | 171109T002000Z | Hawkings |
| 086 | C# .NET Core | 171108T142735Z | Wakawaka |
| 312 | Perl 5 | 171106T174132Z | Nahuel F |
| 078 | Java 8 | 171107T122120Z | Kevin Cr |
| 084 | Excel VBA | 171107T160539Z | Engineer |
| 062 | Octave | 171106T205007Z | Alan |
| 551 | PHP | 171106T224805Z | Titus |
| 094 | Bash | 171106T213641Z | Nahuel F |
| 042 | Ruby | 171106T213255Z | Reinstat |
| 033 | QBIC | 171106T213027Z | steenber |
| 100 | Haskell | 171106T192922Z | colossus |
| 039 | Perl 6 | 171106T182351Z | Sean |
| 064 | Funky | 171105T224739Z | ATaco |
| 016 | MATL | 171106T143714Z | Cinaski |
| 013 | Pyth | 171106T143242Z | Steven H |
| 021 | Alice | 171106T133557Z | Martin E |
| 010 | Ohm v2 | 171106T001726Z | Nick Cli |
| 102 | Lua | 171105T225805Z | Jonathan |
| 065 | Mathematica | 171105T225438Z | ZaMoC |
| 079 | Python | 171105T224305Z | Uriel |
| 014 | Pyth | 171105T223722Z | Maltysen |
AWK, 60 bytes
{for(srand();b!~$0;)b=b sprintf("%c",int(rand()*94+32))}$0=b
{for( # loop
srand(); # seed random
b!~$0; # while $0/input not substr of b
)b=b # craft string
sprintf("%c", # append char
int(rand()*94+32)) # random ascii char
}$0=b # set output
Nim, 93 92 bytes
import random,re;randomize()
proc(s=""):auto=(var r="";while s.re notin r:r&=rand' '..'~';r)
Try it online! (with let m= header to call the lambda)
Thunno 2, 11 bytes
""($Ƈ~;kcɼ+
Explanation
""($Ƈ~;kcɼ+ # Implicit input
"" # Starting with an empty string,
($Ƈ~ # while the input is not in the top of stack:
;kcɼ+ # add a random printable ASCII character
# Implicit output
Japt, 16 14 11 bytes
;_øU}a@P±Eö
;_øU}a@P±Eö :Implicit input of string U
_ :Function taking a string as argument
øU : Contains U
} :End function
a@ :Get the first result of the following function that returns true
P± : Append to P (initially the empty string)
; E : ASCII
ö : Random character
Emacs Lisp, 113 bytes
(lambda(a)(let((case-fold-search)(b""))(while(not(string-match a b))(setq b(concat b(string(+(random 95)32)))))))
Try-it-online version has an additional byte for printing purposes.
(case-fold-search) assures string-match proper casing, it can be omitted
depending on your Emacs configurations (18 bytes).
Better readable version:
((lambda (a)
(let ((case-fold-search) (b ""))
(while (not (string-match a b))
(setq b (concat b (string (+ (random 95) 32)))))
)))
Charcoal, 15 14 12 bytes
W¬№ωθ≔⁺ω‽γωω
Try it online! Link is to verbose version of code. Edit: Saved 2 bytes due to a subsequent bug fix in Charcoal. Explanation:
θ Input string
ω Predefined variable `w`
№ Count number of occurrences
¬ Logical not
W Loop while true
ω Predefined variable `w`
⁺ Concatenated with
γ Predefined printable characters
‽ Random element
≔ ω Assign to predefined variable `w`
ω Predefined variable `w`
Implicitly print
PowerShell, 55 73 bytes
+18 thanks to mazzy bringing it up to spec
for(;$args-cne-join($b+=[char](32..126|random))[-"$args".Length..-1]){}$b
First, it picks randomly an int from the printable range, casts that to a character, and appends it to our accumulator string. The monkeys will always form the word with the last character so keeps going until the ending of our generated string is the target word. So with that fact, it then compares the last n characters joined into a string (via reserve indexing) to the target.
R, 79 76 75 bytes
-3 bytes thanks to MickyT for changing the random sampler
-1 byte thanks to Robin Ryder for tweaking the random sampler again
function(S){G=""
while(!grepl(S,G))G=paste0(G,intToUtf8(32+95*runif(1)))
G}
Brachylog, 17 bytes
I⁰∧Ẹ{sI⁰&|;Ṭṛᵗc↰}
I⁰ The global variable I⁰
is the input,
∧ and
Ẹ starting with the empty string
{ ↰} call this sub-predicate again
ṛ with a random
Ṭ printable ASCII character
; ᵗc appended to the string we're building
| unless
I⁰ I⁰ (which is the input)
s is a substring of the string we've been building
& in which case the string is output.
Can randomly stack overflow. This makes use of two recently added features in Brachylog: global variables, and the apply-to-tail metapredicate ᵗ.
JavaScript 91 bytes
for(b="",a=prompt();b.search(a)<1;b+=String.fromCharCode(~~(Math.random()*95+32)));alert(b)
Jelly, 17 bytes
³ẇ®¬
ḟµ;©ØṖX¤ø¢¿Ḋ
Explanation
³ẇ®¬ First link
³ Program input...
ẇ ...is a contiguous sublist of...
® ...the register?
¬ Logical NOT
ḟµ;©ØṖX¤ø¢¿Ḋ Main link
ḟ Filter: With only one input this returns the empty list. Note ⁸ doesn't work here.
µ New monadic chain
ØṖX¤ Random (X) element from printable ASCII (ØṖ).
; Concatenate with the above
© Copy the result of the concatenation with the above to check if program input is a sublist.
ø New niladic chain.
¢ Calls first link.
¿ Repeat the monadic chain until the first link returns false.
Ḋ Removes the first element of the result.
For some reason (???) the integer 0 is always at the beginning of the result which needs to be removed.
Tested with one and two character inputs (two character sometimes takes a long time or seems to stall) but should work in theory for any length input.
The recursive solution I came up with below is slightly longer.
Jelly, 19 bytes
³ẇ®¬
;©ØṖX¤ßµ¢¡
ḟ`Ç
05AB1E, 10 9 bytes (-1 @ Emigna)
[žQΩJD¹å#
[ | Loop forever.
žQ | Push 0x20-0x7E as a single string.
.R | Pick from it randomly.
J | Join stack (B) with new char.
D | Duplicate (B).
¹å | Push input (A) and check if input (A) is in (B).
# | If the previous statement is true, break loop.
Pushy, 20 18 bytes
LFZ^tCN[,` ~`U'x?i
The program keeps a stack len(input) characters long, and constantly removes the first and appends a new random char, until the initial input string is reached. Each character is printed as it is added, creating the desired effect.
Explanation:
\ == SETUP ==
F \ Put input on second stack
L Z^tC \ On the main stack, make length(input) copies of 0
N \ Remove printing delimiter (newline by default)
\ == MAIN LOOP ==
[ \ Infinitely:
, \ Pop the first item on stack
` ~`U \ Add a new random character (between 32 and 126)
' \ Print this new character
x? \ If the stacks are now equal:
i \ Exit program
C, 192 bytes
i;g(s,b,i,n,j)char*s,*b;{for(b[i+1]=0;b[n+j];++n)s[n]-b[n+j]&&(n=-1,++j);return n;}f(char*s){char*b=calloc(strlen(s),1);for(i=0;s[i];)i=(b[i]=putchar(rand()%95+32))-s[i]?i?g(s,b,i,0,0):0:i+1;}
It's a mess now, but at least it works even for the corner cases...
C, 63 62 61 bytes
Thanks to @Jonathan Frech for saving a byte!
i;f(char*s){for(i=0;s[i=putchar(rand()%95+32)-s[i]?0:i+1];);}
Javascript 74 bytes
s=(a,b='')=>~b.search(a)?b:s(a,b+String.fromCharCode(32+Math.random()*95))
call like this:
s('hi')
GNU sed + coreutils, 75 + 1(r flag) = 76 bytes
h
:
s:.*:shuf -i32-126|dc -e?P:e
H;g
s:\n::2g
/^(.+)\n(.*)\1/{s::\2\1:;q}
b
Try it online! (It takes a lot of runs to get an answer for a length 2 input, because most of the time you run out of allowed TIO computation time.)
Explanation:
h # copy input string 'A' to hold space
: # start loop
s:.*:shuf -i32-126|dc -e?P:e # run shell script: shuf outputs a rnd permutation
#of the set of numbers from 32 to 126, and '?P' in
#dc converts the 1st read decimal to an ASCII char
H;g # append char to hold space ('A\n.'), then copy
#result back to pattern space
s:\n::2g # remove all '\n's from pattern space, but first
/^(.+)\n(.*)\1/{ # if pattern is 'A\n.*A' (A substring of B), then
s::\2\1:;q # search previous regex used and leave only '.*A',
#then quit (implicit printing before exit)
}
b # repeat loop
Benchmark: approximate, for scaling purposes only
- input length: 1, 10 random inputs (runs), average time: < 1 s
- input length: 2, 10 random inputs (runs), average time: 90 s
- input length: 3, 10 random inputs (runs), average time: lots of hours!
C# (.NET Core), 86 bytes
a=>{var b="";for(var r=new Random();!b.Contains(a);b+=(char)r.Next(32,127));return b;}
I don't really like how much creating the Random instance takes, but I don't think there's a way around it.
Java 8, 81 79 78 bytes
a->{String b="";for(;!b.contains(a);b+=(char)(32+Math.random()*95));return b;}
-1 byte thanks to @OlivierGrégoire for pointing me to a (big >.<) mistake I've made..
Explanation:
a->{ // Method with String as both parameter and return-type
String b=""; // Result-String, starting empty
for(;!b.contains(a); // Loop as long as the result does not contain the input
b+=(char)(32+Math.random()*95)
// Append a random character to `b`
); // End of loop
return b; // Return the result-String
} // End of method
Excel VBA, 84 bytes
Dim b
Sub m(a)
b=b &Chr(Int(113*Rnd+14))
If InStr(1,b,a)Then [A1]=b Else m a
End Sub
It's a straightforward implementation. Output is to cell A1 in the active sheet
Octave, 62 bytes
t=input(o="");while(~nnz(regexp(o,t)))o=[o,randi(95)+31];end;o
Explanation:
t=input(o=""); % get stdin and define output
while(~nnz(regexp(o,t))) % while no matches
o=[o,randi(95)+31]; % concatenate string with ascii char
end;
o % output result
Many thanks to Luis Mendo for the edits!
PHP, 55+1 bytes
while(!strpos(_.$argn,_.$s.=chr(rand(32,126))));echo$s;
Run as pipe with -nR. Not suitable for TIO cause of probable timeout.
Insert a space between the quotation marks for PHP older than 7.1.
This 51+1 bytes version will fail if input is 0:
while(!strstr($argn,$s.=chr(rand(32,126))));echo$s;
Bash 94 bytes
p=printf\ -v;until [[ $s = *"$1" ]];do $p x %x $[32+RANDOM%95];$p c \\x$x;s+=$c;done;echo "$s"
QBIC, 33 bytes
≈instr(Z,;)<1|Z=Z+chr$(_r32,126|)
Explanation
≈instr( , )<1| WHILE InStr() can't find
; the input (cmd line string argument) as part of
Z the output (Z$, which is printed automatically on exit)
Z=Z+ add to the output
chr$( ) an ASCII character
_r32,126| with a random codepoint between 32 and 126 (incl)
Sample run:
Command line: hi
`;7L3f$qy )H99tZ@>(-Z1efL|Q-5'BE=P8BdX?Lem/fp|G#~WY[ Q4s9r~Af*T})P4`4d$#ud3AiuTwQPFS@8c7_59C$ GlJ%iJ[FA(rNt<y>Hl=r,wSbBB%q!8&#*CixWbnwE."wrZ:R53iKJkN*@hpQt aMj6Mw<KfT@hkik>_k'_>$~3)jl|J!S`n\Yw|lXi:WAKWp?K"F.cAGI/:~uR8*[;Die{]B*]@;Vhjv,$9]Ys:AIdy!j{aXlr:0=txCP-n{/3lgq,;jXaP\]u}.Zl/7eKF+N54[J9^r:>%/.e~*9aK%de>^TW%p%(_uJPvuV%d<&]zu`t;vkEPC>7pofok0Kj}j?9G{TUxSccth)[)J>@'E)NMzA(i!UV7%;$.Z#j3q@#9Q%}<" &VsbL*<SrG-$NC MAQ\`iIT+.P|5}nv )FZl5_.Kc*AUV9u&fvk.USt3Y!s7^UEL{|D$k#k8.9Fgqn#3dgr(0G.gw1#j$HfU3a|)3-O(d<)<A|`%pJ^/\{[w[H4N/>8J@z/YNsU,zY4o*X+h\Dy!~)Xr8.3"%.39v0d5_-8QBYR".Z]MZ}N>9e?f@hj#hor1IhJ[krrHOamRPxQC>^'dOh,cF_e2;8R@K**Jsx_~t9r~4J$Y;kPsb)8w~:o-`@MwP]OA{8yD%gL(=&M>$nTKw] R[}rS|~}&*gD 'g|gRSDLld+`,,}(1=]ao3Z%2*?wlqU$7=$8q$Fig:7n&+XKQ LV/Aq?BYl_*Ak-PqI$U_>`/`-yD5.3|Zg>,mC"RC`IV^szu:I;0ntn(l'/ZnK}T&i)9!zkd?7Ec/X+IN=-5wwsSV@^<?:K=9m%*@C;zDjc%:d>/E@f7@0NVt4Vz/E;8*0A25F1:JUQ/G#2)un9hQI>2^}&+cY+JP*-#$p+cFs}R|>E;w#q>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kcJ8D<uLAr$dbc$Hl'2KTUlbrd8kD{B0Eeu<&oL2s.S4@Jo$zVq~BqeLsb;k-NG/'iU|)W_:X-.XUc<v\elx57ZZ"R!y_yzve*Wlt>.fE,#Eh:(#gn1kSQ+/3NYjD']I;"+@pnW[1EA.AyqM4,0,dJt.?r2oab.j\k@)BsZ|s39FdL87xyuJ0nXX=yz^~W,}acDZp8ukCpv^<^{CkRS<=GsS$}#fbP5%A$GHdg)+WZLLN9[ue073Q!F"J;X^49*$R'W%C.r~Fj&B`)tq[01a4En%H,kvyZG|,)%$44rJg[tq<wG9FjN<m@larki#;Bns%D}v_efPRH(OeRq0{=>Uc[~xcTcV_9|k54Q2*N.3]LlmFasY3"p =$$onbg$M+ReRsnH|9gV~#2?c1-V$35."DZH-O$~,c.gs]%,]p4\OFIW%l:,E,YT8FCeU8hy#lNq1lCpS 0I&q_*q>|=,(-dHuzi~6$GW22*A\w*&R< W`$HPRr,2A}3w\"Y?d%{2^xP:GqI\26A|.e'H2Z[M4=P.H87O~{)9|B*tHAC\j^S,StW!*snsz82R!:eD@uB4x+x&zSIN(3V|.^N_$=i=p}iK4h'v"$:I<t e:Y/XrSOF83=lkVNa0^k@jB@{ARE@r=Bja`(Bw>@?+`Wo,= u5HhXPeRMXS4@H#$-Jwg2"2-]%7p.o2Ar9J6Y1Ra?"3<oee&bpO^O{nw9=%\0brVNXrelWGoJyb/5W%MB0UBaPsc*29K<N~``NriWM$"eY0@xh^<$b:E/J~S%{#ry~6d?4Vv@^&9'=iBA#2U]bj9>UoJ#wQDN~6cB&/_Pu-XF?_hu3><(M7RW\%Ly@rTC9^b`?kVL~w%[{!&{#aS7<mc@J>ZaN7s}Y.c0:Y.\d&_[L{m|>|>%J^@!i9y0_lwejChi
Haskell, 100 bytes
import System.Random
s#(a:b)|and$zipWith(==)s$a:b=s|1>0=a:s#b
m a=(a#).randomRs(' ','~')<$>newStdGen
Basic idea is to generate an infinite list of characters with randomRs and stop it once we find the string.
Perl 6, 39 bytes
{("",*~(" ".."~").pick...*~~/$_/)[*-1]}
(...)[*-1] returns the last element of the sequence defined by ..., of which:
""is the first element;* ~ (" " .. "~").pickgenerates the next element by appending a random character in the appropriate range to the previous element; and* ~~ /$_/is the ending condition, which is that the current element matches the main function's input argument$_as a literal substring.
Funky, 64 bytes
s=>{S=""whileS::sub((#S)-#s)!=s S+=S.char(math.random(32,126))S}
This uses a few tricks I've been wanting to use in Funky, like a variable name after a keyword as in whileS, and using the fact that strings implicitly parent to the string library.
Ungolfed
function monkey(target){
monkeyCode = ""
while (monkeyCode::sub((#monkeyCode)-#target)!=target){
monkeyCode += string.char(math.random(32,126))
}
monkeyCode
}
Alice, 21 bytes
/U!?"$~dr@
\idwz K"o/
Explanation
/...@
\.../
This is framework for mostly linear programs that operate entirely in Ordinal (string-processing) mode. The IP bounces diagonally up and down through the program twice, which means that the actual code is a bit weirdly interleaved. The commands in the order they're actually executed are:
i!w" ~"rUd?z$Kdo
Let's go through this:
i Read all input.
! Store the input on the tape for later.
w Push the current IP address onto the return address stack.
This marks the beginning of the main loop.
" ~" Push this string.
r Range expansion. Turns the string into " !...}~", i.e. a string
with all printable ASCII characters.
U Random choice. Picks a uniformly random character from this
string. This will remain on the stack throughout the rest of
the program and will form part of the resulting string.
d Join stack. This takes all strings on the stack and joins them
into a single string and pushes that (it does this without actually
removing any elements from the stack).
? Retrieve the input from the tape.
z Drop. If the joined string contains the input, everything up to
and including the input will be discarded. Otherwise, nothing
happens to the joined string. This means that the result will be
an empty string iff the joined string ends with the input.
$K If the top of the stack is not empty, jump back to the w to continue
with another iteration of the main loop.
d Join the stack into a single string once more.
o Print it.
Ohm v2, 10 bytes
Ý£D³ε‽α@§↔
Explanation:
Ý£D³ε‽α@§↔ Main wire, arguments: a (string)
Ý Push empty string to top of stack
£ Start infinite loop
D³ε‽ If a is a substring of the ToS, break out of the loop
α@§ If not, select a random printable ASCII character...
↔ ...and concatenate it with the ToS
Lua, 99 102 bytes
- Saved a bug thanks to ATaco, which added three bytes.
function f(B)s=string S=""while not(s.find(S,B,1,1))do S=S..s.char(math.random(32,126))end print(S)end
Mathematica, 65 bytes
""//.x_/;x~StringFreeQ~#:>x<>RandomChoice@CharacterRange[32,126]&
-3 bytes from Jonathan Frech
Python, 79 bytes
f=lambda x,s='':x in s and s or f(x,s+chr(randint(32,126)))
from random import*
This is theoretically sound, but will crash early due to python's recursion limits (you can set them further to get longer results)
Python, 84 bytes
from random import*
x,s=input(),''
while x not in s:s+=chr(randint(32,126))
print(s)
This one is ought to work for relatively longer strings, since it doesn't rely on recursion, at the cost of 5 bytes.