37
\$\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 alternate between the two, ad nauseam.

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\$
6
  • 1
    \$\begingroup\$ You can lose the {} braces, but that's about as concise as you can make it. \$\endgroup\$
    – Michael
    Commented Dec 7, 2012 at 16:23
  • \$\begingroup\$ This is cool. What font can be used to get a similar image? \$\endgroup\$
    – daniero
    Commented Dec 9, 2012 at 19:24
  • 2
    \$\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
    Commented Apr 9, 2013 at 13:31
  • 1
    \$\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
    Commented Mar 5, 2018 at 20:49
  • 1
    \$\begingroup\$ It's a general pro tip: for(;;) is always shorter than a while \$\endgroup\$
    – Kaddath
    Commented Jul 13, 2021 at 15:49

70 Answers 70

1 2
3
1
\$\begingroup\$

Mathematica - 36 Bytes

Barebones with no formatting. I don't know why the slashes are different thickness

Grid@RandomChoice[{"/","\\"},5{8,5}]

enter image description here

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Welcome to Code Golf, and nice answer! Looks like the thickness just has to do with the font, since it's only the backslashes that are thicker. \$\endgroup\$ Commented Sep 29, 2022 at 17:44
1
\$\begingroup\$

Python, 66 bytes

import random
while 1:print(chr(47+random.randint(0,1)*45),end='')

Try it online!

Ungolfed:

import random
while True:
    slash_calc = 47 + random.randint(0, 1) * 45
    print(chr(slash_calc), end='')
\$\endgroup\$
1
\$\begingroup\$

TI-Basic, 40 bytes

For(I,1,8
For(J,1,16
Output(I,J,sub("/\",randInt(1,2),1
End
End

Outputs to the entire home screen for monochrome TI-83/84-series calculators. For the color calculators, replace 8 and 16 with 10 and 26 respectively, for +1 byte.

\$\endgroup\$
1
\$\begingroup\$

Easyfuck, 13 12 bytes

ImúUt␗J»␀␒ű€

Decompressed:

$[?%+P.J]@␂/\

How?

$[?%+P.J]@␂/\

$             copy 1st cell into storage
 [      ]     while loop
  ?%+         set 1st cell to 1 or 2
     P.J      move pointer by signed value of 1st cell, print, move pointer to 1st cell
         @    beginning of initializer data
          ␂/\ initializer data (2, 47, 92)
```
\$\endgroup\$
1
\$\begingroup\$

Pip, 9 bytes

W1ORC"\/"

Attempt This Online!

There's an orc in my code!

Explained

W1ORC"\/"­⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏⁠‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌­
W1         # ‎⁡Forever:
  O        # ‎⁢  Print
   RC"\/"  # ‎⁣  A random character from "\/" 
💎

Created with the help of Luminespire.

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

Swift, 47 bytes

import Darwin
while putchar(rand()%2*45+47)>0{}

This version is macOS-only; replace Darwin with Glibc if you're on Linux (this actually saves a byte). If you're in doubt, Foundation works on most platforms that support Swift.

This is pretty much the most direct port of the C answers you can get.

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

YASEPL, 16 bytes

=a¢0,1*45+47›?1

explanation:

=a¢0,1*45+47›?1          packed
=a¢0,1                   create a random number between 0 and 1
      *45                times it by 45
         +47             add 47 
            ›            print it out in char form (without a newline)
             ?1          loop back to the first instruction
\$\endgroup\$
0
\$\begingroup\$

F# (.NET Core), 6967 bytes

let r=System.Random()
while 1=1 do printf$"{char(92-r.Next(2)*45)}"

A console window shows the correct output. In the background, Visual Studio 2022 displays the submitted code.

Try it online: This code is not fully golfed - interpolated strings are a new(-ish) feature to .NET that TiO doesn't support.

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

Fortran (GFortran), 49 bytes

2 call fput(char(47+45*int(2*rand(0))));goto2
end

Try it online!

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

Thue, 26 bytes

a::=ab
b::=~\
b::=~/
::=
a

Try it online!

Makes use of Thue's nondeterminism.

There are three rules, thr first replaces a to ab, the second and third removes a b and prints a / and . The second and third rules have equal probability to be run.

For some reason this program doesn't work on TIO, so the TIO link is link to the Thue Python interpreter with the input set to the Thue program.

Output: output

\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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