Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

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.

Is there a way in PHP to write this more compactly, perhaps getting rid of the while block?

share|improve this question

migrated from Dec 8 '12 at 14:36

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

You can lose the {} braces, but that's about as concise as you can make it. – Michael Dec 7 '12 at 16:23
This is cool. What font can be used to get a similar image? – daniero Dec 9 '12 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 Apr 9 '13 at 13:31

15 Answers 15

up vote 3 down vote accepted

The goto operator was added to PHP from version 5.3.0 so you could use the same method as you would in BASIC:

a: echo chr(47 + 45 * rand(0,1)); goto a;
share|improve this answer

Since this has been migrated to codegolf...

PHP 30 bytes

<?for(;;)echo rand(0,1)?~Ð:~£;

The Ð is character 208, and the £ is character 163.

Sample usage (on a Windows box):

color 18 & php maze.php

Produces something similar to:

It works best with a monospace font that is exactly square (here I've chosen the standard system font 8x8). To go back to your default color, you can type color again without any parameters.

share|improve this answer
Nicely done, thanks! – Judy Dec 8 '12 at 17:04
Can save an extra byte with rand()%2 (nobody stated how pseudo-random it had to be) - Also doesn't work on Unicode terminals (like OSX) – Leigh Jan 9 '13 at 17:16
@Leigh Due to its implementation, rand()%2 will alternate between zero and one. mt_rand()%2 would work, however. – primo Jan 9 '13 at 17:54


Lacking PETSCII, I rolled my own "\" and "/".

No cigar for succinctness here.

Graphics[{Thickness[.0055], RGBColor[0.73, 0.55, 1.], Line /@ Flatten[Table[RandomChoice[{{{x, y}, {x + 1, y + 1}}, {{x + 1, y}, {x,  y + 1}}}], {x, 40}, {y, 25}], 1]}, Background -> Blue]

blue maze

share|improve this answer

Brainfuck - 534


My prng (from here) is both large and extremely slow. Perhaps a simpler LFSR or similar would suffice, but this works:

enter image description here

share|improve this answer

C, 39 chars (38 on MSVC)


enter image description here See it run.

On MSVC, we can replace putchar() with _putch() and save a byte, but it doesn't work in IDEOne.

share|improve this answer

ruby, 27 23 chars

loop{$><<"/\\"[rand 2]}

$><< is 'print to stdout'.

share|improve this answer
You should be able to replace "/\\" with '\/'. Ruby 1.8 requires [rand(2),1] to produce the character instead of the ascii code. – primo Dec 18 '12 at 10:22

C++, 45 Chars

int main(){for(;;)cout<<(rand()%2?"/":"\\");}

Not going to win any awards for shortness, but I had already written this when I heard about the mentioned book, so I just golfed it.

The putchar trick also works in C++, getting you down to 43, but you can't avoid declaring the return type on main.

share|improve this answer

i try create using css style, and it's work

  body {
    font-family: monospace;

this php code :

  while($i) {
    if($i%200 == 0) echo '<br/>';
    echo chr(47 + 45 * rand(0,1));
share|improve this answer
I'd like to suggest an alternative style: font-family: courier; line-height: 0.75em; letter-spacing: -0.25em; Using -0.25em for letter-spacing seems to match up better than -3px, which has artifacts due to an inexact pt to px conversion. – primo Dec 13 '12 at 5:14

Not much better. Needs php 5.5+ for the array dereferencing feature.

while(1) { echo ['/', '\\'][rand(0, 1)]; }
share|improve this answer
Never even thought of that, thanks! – Judy Dec 7 '12 at 16:29

print has a return value of 1, so if you use that you can just wrap the whole expression in the while:

while(print chr(47 + 45 * rand(0,1));

You can probably golf it further too.

share|improve this answer

Common Lisp - 68

(defun s(l)(elt l(random(length l))))(loop do(format t"~a"(s"/\\")))
share|improve this answer

Python, 68

In the "my language sucks at this" category, we've got Python!

import random,sys
while 1:sys.stdout.write(random.choice('/\\'))

Thanks to Ivo for a few chars on imports and choice.

share|improve this answer
Well, you're not saving anything with 'from sys import*' - if you instead write 'import sys,random' and use sys.stdout and random.random, you'll save 5 bytes. – Ivo Jan 10 '13 at 11:55
Python3: import random while 1:print(random.choice('/\\'),end='') – Ivo Jan 10 '13 at 12:02
@Ivo, Thanks, I forgot about choice! You can keep py3, though. ;) – boothby Jan 12 '13 at 4:49

I guess you can use recursion.

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

I guess you can use recursion.

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

share|improve this answer
stack space is not infinite. – codaddict Dec 7 '12 at 16:25
@codaddict so it will timeout. – Neal Dec 7 '12 at 16:26
Assuming a considerably high timeout your snippet is not equivalent to the OP's. – codaddict Dec 7 '12 at 16:31
@codaddict lol the OPs times out as well. it is an infinite loop! – Neal Dec 7 '12 at 16:31
Yours would create a run time error before the timeout. – codaddict Dec 7 '12 at 16:33

chopped off one character:

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

then remove the curly braces:

while(1) echo chr(2+45*rand(1,2));

another trick, with the same length:

while(1) echo chr(rand()%2*45+47);
share|improve this answer
Due to the algorithm it employs, rand()%2 will result in an alternation of 0s and 1s. Not incredibly random. You'd need to use rand(0,1) or mt_rand()%2 instead. – primo Dec 8 '12 at 15:06


This one looks funny for me :

perl -pe '$_="~"ge$_?"/":"\\"' </dev/urandom

(Sorry to be out of subject here. I know this is not PHP)

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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