Take the 2-minute tour ×
Programming Puzzles & Code Golf Stack Exchange is a question and answer site for programming puzzle enthusiasts and code golfers. It's 100% free, no registration required.

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.

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

share|improve this question
1  
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
1  
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
add comment

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

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

15 Answers

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
add comment

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
add comment

Mathematica

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
add comment

C, 39 chars (38 on MSVC)

main(){while(putchar(rand()%2?47:92));}

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.

main(){while(_putch(rand()%2?47:92));}
share|improve this answer
add comment

ruby, 27 23 chars

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

$><< is 'print to stdout'.

share|improve this answer
1  
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
add comment

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
add comment

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

<style>
  body {
    font-family: monospace;
    line-height:75%;
    letter-spacing:-3px;
  }
</style>

this php code :

<?php
  $i=10000;
  while($i) {
    if($i%200 == 0) echo '<br/>';
    echo chr(47 + 45 * rand(0,1));
    $i--;
  }
?>
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
add comment

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
add comment

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
add comment

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
add comment

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
add comment

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
1  
@codaddict lol the OPs times out as well. it is an infinite loop! –  Neal Dec 7 '12 at 16:31
1  
Yours would create a run time error before the timeout. –  codaddict Dec 7 '12 at 16:33
show 2 more comments

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
add comment

Perl

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
add comment

Common Lisp - 68

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

Your Answer

 
discard

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.