23
\$\begingroup\$

Just had a 'spirited' conversation with a co-worker about the succinctness of the following BASIC statement:

10 PRINT CHR$(205.5+RND(1)); : GOTO 10

It's the title of this book, and will simply print a sequence of / and \ characters, alternating between the two randomly, resulting in a pattern similar to this:

enter image description here

(Image borrowed from http://www.flickr.com/photos/rndmcnlly/5058442151/sizes/o/in/photostream/)

Being of a PHP proclivity, we wondered what the most compact way of writing the same thing in PHP would be, and came up with this:

while(1) { echo chr(47 + 45 * rand(0,1)); }

chr(47) is a / character, and chr(92) is a \. So the statement echo chr(47 + 45 * rand(0,1)); will randomly alternative between the two, ad nauseum.

In a language of your choosing, write the shortest program or function to output an infinite random sequence of \ and / characters, where each character has an equal probability of being chosen.

\$\endgroup\$

migrated from stackoverflow.com Dec 8 '12 at 14:36

This question came from our site for professional and enthusiast programmers.

  • 1
    \$\begingroup\$ You can lose the {} braces, but that's about as concise as you can make it. \$\endgroup\$ – Michael Dec 7 '12 at 16:23
  • \$\begingroup\$ This is cool. What font can be used to get a similar image? \$\endgroup\$ – daniero Dec 9 '12 at 19:24
  • 1
    \$\begingroup\$ This (picture) reminds me of the first "program" I ever wrote on my commodore 64 straight out of the manual :) ah happy days \$\endgroup\$ – Dale Apr 9 '13 at 13:31
  • \$\begingroup\$ @daniero I think the actual typeface is called Terminal, but I found it online as "Windows Command Prompt" codepen.io/nickforddesign/pen/WMqQdM?editors=0110 \$\endgroup\$ – nickford Mar 5 '18 at 20:49
  • \$\begingroup\$ What a strange coincidence that this Code Golf repo on Github happens to have the exact same challenge... github.com/noops-challenge/golfbot \$\endgroup\$ – Geza Kerecsenyi Jul 20 at 18:51

45 Answers 45

1
\$\begingroup\$

Cubix, 10 bytes

Du\'^>o$/;

Watch it run

    D u
    \ '
^ > o $ / ; . .
. . . . . . . .
    . .
    . .

Was an interesting one to do as i haven't played with the random direction generator much.

  • ^D redirect to the random direction pointer

Directed north

  • o\> loops around the cube, print empty stack and redirecting back into the print commands (stack is empty)

Directed west

  • ^ redirected back into the random direction pointer

Directed south

  • \'/> reflected to the east, push / onto the stack and redirect into the print commands

Directed east

  • u'\> uturn to the right, push \ onto the stack and redirect into the print commands

Print commands

  • o$/;^ output top of stack, skip over the reflect, pop from the stack and redirect into the random direction pointer
\$\endgroup\$
1
\$\begingroup\$

Keg, 12 bytes

You do not have to worry about the evenness 0f the output, because the range is 0 to 32767, and 32767+1 = 32768; it is an even number.

{~2%[\\|\/],
\$\endgroup\$
0
\$\begingroup\$

Tcl, 53 bytes

while 1 {puts -nonewline [expr rand()<.5?"/":"\\\\"]}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pyth - 12 9 bytes

#@,\/\\O2

Tested without loop code because the online interpreter dislikes infinite loops

Explanation:

#@,\/\\O2
#             Repeat until Error
 @            Implicitly print item number
       O2     random number between zero and two exclusive
  ,           of two item list containing
   \/         One character string /
     \\       One character string \
\$\endgroup\$
0
\$\begingroup\$

Add++, 24 bytes

+1
W,R2,-1,*45,+47,H,x:1

Try it online!

\$\endgroup\$
0
\$\begingroup\$

q/kdb+, 18 bytes

Solution:

while[1;1"\\/"1?2]

Explanation:

A simple while(true) loop...

while[1;1"\\/"1?2] / the solution
while[1;         ] / while true do ...
              1?2  / 1 choose 2 (0 or 1)
         "\\/"     / string \/, index in at 0 or 1
        1          / print to stdout without newline

Extras:

Another 18 byte solution: {1"\\/"1?2}\[::;1]

\$\endgroup\$
0
\$\begingroup\$

Yabasic, 28 bytes

Mid$("/\\",...) is used over Chr$() as the character codes for / and \ are 47 and 92, respectively.

While the GoTo structure of the original could easily be used for this answer, Do...Loop ends up being shorter due to the the structure of the Mid$ call.

Do?Mid$("/\\",Ran(3),1);Loop

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Japt, 9 bytes

iOo"/\\"ö

Try it

\$\endgroup\$
0
\$\begingroup\$

MathGolf, 8 bytes

1Æû\/wq∟

Try it online!

Explanation

1          push 1
 Æ     ∟   do-while-true without popping using 5 operators
  û\/      push "\/"
     w     random char from string
      q    print without newline

Implicit popping doesn't seem to be working as intended when using do-while-true, or any other loop type. With this working correctly, the first 1 could be removed.

For a "working" 7-byter, you could do t{û\/wq, which replaces the do-while-true with a simple for loop, and begins the script by pushing the current timestamp in milliseconds to the stack. Currently, that means about \$1.56*10^{12}\$ iterations, which is "infinite" for most applications. However, this is not a valid answer, so the official answer is still 8 bytes.

\$\endgroup\$
0
+100
\$\begingroup\$

APL (Dyalog Unicode), 12 bytes

→≢⍞←'/\'[?2]

Try it online!

Tradfn body that prints indefinitely.

Thanks to @Adám for 5 bytes, and for pointing out that, with a little bit of CSS magic, TIO can print the actual pattern like in the image.

How:

→≢⍞←'/\'[?2] ⍝ Tradfn body
         ?2  ⍝ Roll (randomly select) an integer between 1 and 2
        [  ] ⍝ Use the result to index...
    '/\'     ⍝ ...the string containing the two slashes
  ⍞←         ⍝ Print it to the same line
 ≢           ⍝ Tally the result (yields 1)
→            ⍝ then 'goto' that line
\$\endgroup\$
  • \$\begingroup\$ This is eligible for 200 rep, no? \$\endgroup\$ – Adám Jul 24 at 20:18
  • \$\begingroup\$ @Adám is it? I've no idea why it would. \$\endgroup\$ – J. Sallé Jul 24 at 20:37
  • \$\begingroup\$ This. \$\endgroup\$ – Adám Jul 24 at 20:38
  • 1
    \$\begingroup\$ Oh, I see. I guess it is, then? There were no answers in any APL flavour before mine, and it meets all the conditions in the post. I'll add my request to it. \$\endgroup\$ – J. Sallé Jul 24 at 20:49
  • \$\begingroup\$ Cool, and feel free to dig up any of your posts from since February. I have way too much rep. \$\endgroup\$ – Adám Jul 24 at 20:51
0
\$\begingroup\$

Runic Enchantments, 13 bytes

`/-R`A*+k$!;

Try it online!

Slightly ungolfed for readability, with spaces to separate operational segments:

'/  2'RA  '- *+k $ !;

The difference of 45 is encoded in the - char literal, as chars are implicitly converted to integers when fed into a operator expecting a number value.

2'RA randomly generates a 0 or a 1, multiplied against the - and added to the / char resulting in either / or \, which is then printed after being coerced back to a char from an int. In the golfed version the  (0x02) serves as the 2.

Then the program loops forever by skipping the terminator, !;.

\$\endgroup\$
0
\$\begingroup\$

><>, 12 bytes

x!\"/!
^o< <

Try it online!

The idea here is to use x in combination with the ^ below to both loop and choose a random horizontal direction. If the IP goes left from the x, it will skip the mirror / and quote the string "\!x!/", reflect upwards with \, wrapping around to the <, which redirects the IP to output the result and begin the loop again. If the IP goes right from the x, a similar thing will happen.

This is all thanks to the nice happenstance that / and \ are control flow modifiers in ><>, allowing us to save a ".

An alternative solution for 13 bytes is slightly more generic and generates a lot fewer side effects.

xa!o"/\"o!a
^

(You can replace the as with spaces; I have them placed here for clarity. In production code, you would want to do this replacement to avoid unnecessary stack population.)

\$\endgroup\$
0
\$\begingroup\$

Python, 61 bytes

from random import*
while 1:print('/\\'[randint(0,1)],end='')

Pretty self explanatory. Had to use the double backslash since backslashes are escape characters in Python.

Try it Online!

\$\endgroup\$
0
\$\begingroup\$

R, 33

Just interested to see whether this can be made smaller.

while(1)cat(sample(c('/','\\'),1))
\$\endgroup\$
0
\$\begingroup\$

Clojure, 34 bytes

(loop[](pr(rand-nth"\\/"))(recur))
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.