35
\$\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
    Dec 7, 2012 at 16:23
  • \$\begingroup\$ This is cool. What font can be used to get a similar image? \$\endgroup\$
    – daniero
    Dec 9, 2012 at 19:24
  • 1
    \$\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
    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
    Mar 5, 2018 at 20:49
  • \$\begingroup\$ What a strange coincidence that this Code Golf repo on Github happens to have the exact same challenge... github.com/noops-challenge/golfbot \$\endgroup\$ Jul 20, 2019 at 18:51

61 Answers 61

1 2
3
0
\$\begingroup\$

Rust, 143 bytes, std only

use std::hash::*;fn main(){loop{print!("{}",b"/\\"[std::collections::hash_map::RandomState::new().build_hasher().finish()as usize%2]as char);}}

Attempt This Online!

This mess is because the Rust stdlib (at the time of writing) has no "proper" RNG API. So, I had to use the workaround described here to get a random number.

\$\endgroup\$
1 2
3

Your Answer

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

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