# 10 PRINT CHR$(205.5+RND(1)); : GOTO 10 in PHP (or anything else, for that matter) 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:

(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.

• You can lose the {} braces, but that's about as concise as you can make it. Commented Dec 7, 2012 at 16:23
• This is cool. What font can be used to get a similar image? Commented Dec 9, 2012 at 19:24
• This (picture) reminds me of the first "program" I ever wrote on my commodore 64 straight out of the manual :) ah happy days
– Dale
Commented Apr 9, 2013 at 13:31
• @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 Commented Mar 5, 2018 at 20:49
• It's a general pro tip: for(;;) is always shorter than a while Commented Jul 13, 2021 at 15:49

# Mathematica - 36 Bytes

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

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


• 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. Commented Sep 29, 2022 at 17:44

# 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='')


# 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.

# 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) $$$$  # 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. # 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. # 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  # F# (.NET Core), 6967 bytes let r=System.Random() while 1=1 do printf$"{char(92-r.Next(2)*45)}"


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

# Fortran (GFortran), 49 bytes

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


Try it online!

# 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: