
A single line of Basic sends readers into a labyrinth - munishdayal
http://www.slate.com/articles/technology/books/2012/11/computer_programming_10_print_chr_205_5_rnd_1_goto_10_from_mit_press_reviewed.html
======
akavi
A version that works in bash:

yes 'c=(╱ ╲);printf ${c[RANDOM%2]}'|bash

Source and credit: <http://stackoverflow.com/a/13612327>

~~~
lloeki
The one-line shell command python version ended up being surprisingly painful:

    
    
        echo "# -*- coding: utf-8 -*-\\nfrom random import choice\\nwhile(True):\\n  print(choice(\"╱╲\"), end='')" | python3
    

or

    
    
        python3 -c 'exec("from random import choice\nwhile(True):\n  print(choice(\"╱╲\"), end='"''"')")'
    

(Oh, the fun of deeply nested quote escaping!)

The _exec_ / _echo_ thing being required since we need multiple lines, because
while ' _;_ ' is the statement separator python barfs on " _import foo; while
(True): pass_ ". Bug or spec?

~~~
lifthrasiir
It is a spec. Python in general prefers a statement over an expression, so the
while statement does not have an expression form.

~~~
dalke
Python has two types of statements; "simple" statement and "compound"
statements. Compound statements are those, like the if- and while-statements,
which require multiple lines.

Only simple statements may be separated by a ';', not compound statements.

This is a bit different than what you wrote. For example, "x=4" is a
statement, and not an expression, but "x=4;y=5;z=6" is a valid Python line
because assignment is a simple statement, and multiple simple statements may
be separated by a ';'.

------
jonmrodriguez
Another fun example of a computer generating mezmerizingly complex art from
simple code is that as a kid, I wrote a Langton's Ant implementation in QBASIC
on a DOS machine (don't remember the version). IIRC this was using Graphics
Mode 1 (320 x 200 pixel framebuffer), and I used PSET and PRESET to draw white
and black pixels. This so far sounds ordinary but:

The cool thing was that I forgot to write the bounds check to make sure the
ant stayed inside the framebuffer! So the ant would happily trundle off-
screen, encounter arbitrary bits that were in that memory, and would often
eventually return back on the screen from a different location. Or sometimes
(probably due to encountering zeros offscreen) it would run a lap around the
edge of the screen and then veer back inward.

Langton's Ant: <http://en.wikipedia.org/wiki/Langtons_ant>

~~~
chengsun
I had the same bug (no pun intended), but in JavaScript. It does produce some
interesting effects after making a few laps.

<http://chengsun.github.com/walk.html>

I still am a "kid", and it amuses me that I'm making the same stuff on modern
platforms that have already been done on DOS machines by kids like me.

~~~
jonmrodriguez
Awesome!! Glad you were able to reproduce the effect! :D

------
rossjudson
Just to be clear...it doesn't generate a labyrinth. It generates something
that looks like a labyrinth, but isn't one. It's just random forward and
backward slashes.

~~~
akkartik
The device you're on doesn't generate text. It looks like text, but it's
really just red, green and blue dots.

~~~
derleth
> The device you're on doesn't generate text. It looks like text, but it's
> really just red, green and blue dots.

Printers don't generate text. It looks like text, but it's really just ink on
paper.

The point is that real labyrinths have properties these pseudo-labyrinths
don't. To be specific:

> In colloquial English, labyrinth is generally synonymous with maze, but many
> contemporary scholars observe a distinction between the two: maze refers to
> a complex branching (multicursal) puzzle with choices of path and direction;
> while a single-path (unicursal) labyrinth has only a single, non-branching
> path, which leads to the center. A labyrinth in this sense has an
> unambiguous route to the center and back and is not designed to be difficult
> to navigate.

<http://en.wikipedia.org/wiki/Labyrinth>

~~~
sp332
"In colloquial English, labyrinth is generally synonymous with maze" means
that yes, this is a labyrinth because "labyrinth" is synonymous with "maze".
"Many contemporary scholars observe a distinction between the two" means "most
people don't observe any distinction between the two."

------
fragsworth
It may be somewhat interesting to analyze the properties of randomly-generated
labyrinths of this type.

For instance, some spaces are clearly enclosed. i.e., there is a finite border
outside which you cannot escape. Some paths seem to run on for a very long
time. Are all paths enclosed? If so, given a random point on a randomly
generated maze, what is the average area of an enclosure?

~~~
rfugger
I don't think a "corridor" ever forks, so it's not really a labyrinth where
there are decisions to be made about which way to go, it's just a bunch of
disjoint long, twisty hallways.

~~~
iliis
Add a space every now an then and it becomes a 'real' maze:

    
    
        #include <stdio.h>
        main() { for(;;) {printf(rand()%2 ? "╱":"╲"); if(rand()%100<1) putchar(' ');} }

------
diego
Can this book not be available in digital form? Amazon and MIT Press only list
the hardcover edition.

<http://mitpress.mit.edu/books/10-print-chr2055rnd1-goto-10-0>

~~~
superprime
The article links to the book's (free) download page: <http://10print.org>

~~~
diego
Thanks, I wonder who is the idiot who downvotes an honest question.

Still, I would pay $9.99 for mobi or epub. PDF is inconvenient for the Kindle
and the iPad. Converting with Calibre is a chore, and rarely yields good
results.

~~~
cubicle67
I find GoodReader excellent for pdfs on the iPad. It's not only a great reader
but also allows annotation etc plus makes getting pdfs into and out of your
iPad fairly painless

------
mjcohenw
I discovered this on my Commodore PET probably about 1980 and presented it at
a users' group meeting (in the Los Angeles area). I have no way to prove this
right now, but I swear that this is true.

------
zaptheimpaler
The article seems to be a a lot of hyperbole without much substance.

The code itself is simple. __CHR$(205.5 + RND(1)) __becomes __CHR$(205) __or
__CHR$(206) __depending on the random number. 205 generates a \ , and 206
generates a /.

~~~
mmphosis

       10 PRINT MID$("/\",1.5+RND(1),1);:GOTO 10
    

_Run it on another old-school computer, like an Apple II, and you won’t get
the same transfixing result, for details that have to do with the Commodore
64’s character set, called PETSCII._

~~~
akkartik
Is it the same as this C snippet?

    
    
      main() { while (1) printf("%c", rand()%2 ? '/' : '\\'); }
    

_Edit:_ Golfed my original snippet down, then put back original after
mmphosis's identical translation.

~~~
activepeanut
Small variant:

void main() { while (1) printf("%c", "/\\\"[rand()%2] ); }

------
pom
Reading this right now. I also made an SVG version at
<http://romulusetrem.us/10print/>

------
rwg
If you have a machine with a UTF-8 terminal and a recent-ish Perl (tested on
Perl 5.10 + Mac OS X 10.6), you can play along at home by running:

    
    
        perl -e '$ |= 1; binmode(STDOUT, ":utf8"); while (1) { print chr(9585.5 + rand()); select(undef, undef, undef, 0.01); }'

~~~
SoftwareMaven
While interesting, it isn't the same. I'm not sure what, exactly, the
difference is, but I don't get the feeling of "labyrinth". Instead, I get the
feeling of a bunch of similar glyphs.

~~~
vacri
Try changing the 0.01 at the end to 0.001 or lower.

------
dwoldrich
That's cool! If you don't have a C64, you can input a similar line of code at
<https://clubcompy.com/?shell>

10 PRINT TOCHAR 13.5 + RANDOM; GOTO 10 RUN

I prefer the hopelessly lost maze variant! :>

10 PRINT TOCHAR 222.5 + RANDOM; GOTO 10 RUN

------
xefer
I've always had a soft spot for mazes. Seeing how a recursive function could
solve a maze was what got me hooked on programmng to begin with. At the time
it was mind-blowing. It was the first time that software seemed like magic.

------
alexleavitt
Highly recommend picking up this book. As a former local colleague of Nick
Montfort, I can say it's going to be a great read.

~~~
ynniv
I recently read Racing the Beam (Monfort, Bogost), a survey of the defining
technical details of the Atari 2600. I've read a number of gaming history
books, and RtB was by far the most interesting. It might be better described
as a hacker history of the platform, focusing on the limitations of the system
and the hacks around them that caused Atari VCS games to have a personality
distinct from its contemporaries. Also highly recommended.

[<http://www.nickm.com/vcs/>]

[<http://www.platformstudies.com/>]

------
p4bl0
Racket version (should work in most Scheme implementation too actually):

    
    
        (let loop () (display (list-ref '("╱" "╲") (random 2))) (loop))
    

Use `racket -e <expr>` tu run it directly, for Guile it should work with
`guile -c <expr>` but it seems that the unicode charaters make it crash
(however it works if you copy the line of code in Guile's REPL rather than on
the command line with -c).

------
taylorbuley
The PDF is free: <http://trope-tank.mit.edu/10_PRINT_121114.pdf>

via [http://lab.softwarestudies.com/2012/11/10-print-book-from-
mi...](http://lab.softwarestudies.com/2012/11/10-print-book-from-mit-software-
studies.html)

------
javajosh
JavaScript (in a browser):

var r = 0; function go(){setTimeout('document.write(r++ % 80 ?
Math.round(Math.random()) ? "\\\\\\\" : "/" : "<br>"); go()', 100)}; go();

This was actually a lot more difficult for me to write than I thought it would
be. `document.write` blocks and also a browser doesn't wrap by default, hence
the r variable.

This will, of course, nuke any page you happen to run it on. :)

P.S. Why is code formatted so badly on HN these days? Until that's fixed I'll
just use normal formatting.

~~~
javajosh
The node version is _also_ hard to write. I tried `process.stdout.write` but
it always prints a newline, at least from the repl.

------
huseyinkilic
13 bytes version in 6502 machine code:

7C 00 20 D2 FF A1 85 29 01 E9 92 D0 F5

(Save this as a .prg file and run it in your favourite C64 emulator or on the
real thing itself. Please note that the first two bytes are actually required
by .prg file format to indicate load address. The actual code is 11 bytes).

For more information:

<http://pouet.net/prod.php?which=60810>

------
anthonyb
Python version:

    
    
      python -c 'import random; print "".join(random.choice(r"/\\") for i in range(80*24))'

~~~
groovy2shoes
Same thing, but with corner-to-corner unicode glyphs:

    
    
        python -c 'import random; print "".join(random.choice([u"\u2571",u"\u2572"]) for i in range(80*24))'

~~~
est
python -c "print ''.join(__import__('random').choice(u'\u2571\u2572') for i in
range(80*24))"

------
LarryMade
If you were like me back in the 80s on a PET you did that and then moved up to
more complex randomness with other graphic symbols like the four corner curves
or bends. I think they had that line in one issue of Creative Computing with a
bunch of other character graphic effect snippets.

------
herbig
You kids and your fancy one liners:

import java.util.Random;

public class Maze {

public static void main(String[] args) {

    
    
    		String[] blah = new String[] { "╱", "╲" };
    
    		Random rng = new Random();
    
    		for (;;) {
    			System.out.print(blah[rng.nextInt(2)]);
    
    		}
    	}
    }

------
joeld42
I remember learning this trick, not sure where I learned it from but this
particular snippet had a huge impact on me. I spent hours and hours playing
with variations on this.

------
keithohara
processing.js version:
<http://studio.sketchpad.cc/sp/pad/view/dWrYcgtqBO/rev.2>

------
teeja
Same fascination with this, I think, as with the simple rules of Conway's.
S'pose there are other such "a-mazing" simple revelations still floating in
the void?

------
anigbrowl
I spent many long hours playing with this on a Vic-20 back in the 80s.
Commodore BASIC was so weird that you pretty much had to hack it.

------
dnm
a place to run the code <http://codeazur.com.br/stuff/fc64_final/>

------
itry
PHP:

while (1) echo rand (0,1) ? "╱" : "╲";

~~~
itry
You can execute it on the command line like this:

php -r 'while (1) echo rand (0,1) ? "╱" : "╲";'

