10 PRINT CHR$(205.5+RND(1)); : GOTO 10 in PHP (or anything else, for that matter)

24

6

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.

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.

Judy

Posted 2012-12-07T16:21:03.537

Reputation: 367

1You can lose the {} braces, but that's about as concise as you can make it. – Michael – 2012-12-07T16:23:46.517

This is cool. What font can be used to get a similar image? – daniero – 2012-12-09T19:24:58.340

1This (picture) reminds me of the first "program" I ever wrote on my commodore 64 straight out of the manual :) ah happy days – Dale – 2013-04-09T13:31:12.160

@daniero I think the actual typeface is called Terminal, but I found it online as "Windows Command Prompt" https://codepen.io/nickforddesign/pen/WMqQdM?editors=0110

– nickford – 2018-03-05T20:49:10.183

What a strange coincidence that this Code Golf repo on Github happens to have the exact same challenge... https://github.com/noops-challenge/golfbot

– Geza Kerecsenyi – 2019-07-20T18:51:30.343

Answers

10

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;

MichaelRushton

Posted 2012-12-07T16:21:03.537

Reputation: 244

18

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.

primo

Posted 2012-12-07T16:21:03.537

Reputation: 30 891

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 – 2013-01-09T17:16:28.370

@Leigh Due to its implementation, rand()%2 will alternate between zero and one. mt_rand()%2 would work, however. – primo – 2013-01-09T17:54:55.680

15

Mathematica 157 bytes

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

No cigar for brevity here.

Graphics[{Thickness[.005],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

DavidC

Posted 2012-12-07T16:21:03.537

Reputation: 24 524

This image does the grey dot optical illusion!! – Magic Octopus Urn – 2018-02-02T20:33:14.900

8

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

captncraig

Posted 2012-12-07T16:21:03.537

Reputation: 4 373

I think this is not random, though, since this is brainfuck. – Erik the Outgolfer – 2016-10-04T13:43:52.337

most of these are relying on built-in prngs. Yeah, this will output the same every time. Could allow it to be seeded from keyboard at cost of a couple extra chars, but thats not really the point here, I think. – captncraig – 2016-10-04T16:22:31.887

5

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));}

JoeFish

Posted 2012-12-07T16:21:03.537

Reputation: 691

5

Common Lisp, 33

(loop(princ(elt"/\\"(random 2))))

coredump

Posted 2012-12-07T16:21:03.537

Reputation: 6 292

3

05AB1E, 7 bytes

["/\"Ω?

Try it online!

["/\"Ω?  - Full program
[          Start an infinite loop
 "/\"      ... Push the string "/\" on the stack
     Ω     ... Pick a character from the top of the stack, at random
      ?    ... Print without a newline

scottinet

Posted 2012-12-07T16:21:03.537

Reputation: 981

3

Befunge, 12 9 8 7 Bytes

Edit: James Holderness figured out an insane solution that uses three quotes instead.

?\","/"

Overflows the stack with a lot of excess characters for each symbol, but who cares when you can golf that 1 byte?

Old version

"/\"?>,#

(Note the trailing space) Trailing space apparently not needed (thanks Mistah Figgins)

Try It Online

"/\" Adds both / and \ to the stack
    ? Chooses a random direction to go in. Both up and down loop back around to the ? so it's 50% chance to go in either direction.
Going left
"/\"  >,#  Adds \ and / to the stack but only print the second, the / before heading back
Going right
      >,# Prints the top of the stack, the \ before heading back

This does start to fill up the stack, with one extra symbol for every symbol printed.

Jo King

Posted 2012-12-07T16:21:03.537

Reputation: 38 234

1

looks like you can get rid of the extra space, as the # is at the end of a line. It's not consistent behavior, but the interpreter you're using allows it: https://tio.run/##S0pNK81LT/3/X0k/RsneTkf5/38A

– MildlyMilquetoast – 2017-11-28T22:40:43.673

I also wonder if you are able to use only one ", but my attempts have been futile. Because you need a / on one side of the ", you will have to divide half the time. – MildlyMilquetoast – 2017-11-28T22:41:14.200

@MistahFiggins Space definitely shouldn't be required for any standard Befunge-93 implementation. The code may fail in some Funge-98 interpreters, where the spec is more ambiguous, but adding a space wouldn't help in those cases anyway - you'd need a < after the # to make it work. – James Holderness – 2017-11-28T23:13:48.837

@MistahFiggins As for getting rid of one of the quotes, you can kind of do that in Befunge-98 by using ' instead. Try it online!

– James Holderness – 2017-11-28T23:21:13.853

@MistahFiggins I managed to get to 11 bytes using only one ". ? #,\"1/,#1 The problem lies with having to use 2 prints – Jo King – 2017-11-28T23:23:24.087

2@JoKing Just realised you can actually do better with three quotes! ?\","/" Only downside it it'll overflow the stack much faster I think. – James Holderness – 2017-11-28T23:44:49.547

@JamesHolderness wow! I had gotten really close by using a high value instead of \ so that it would divide to be \, but I hadn’t thought about using 3. Genius. – MildlyMilquetoast – 2017-11-28T23:51:33.367

3

Befunge-98 (PyFunge), 7 bytes

? '/,\'

Try it online!

the ? sends the IP either left or right, so either / or \ is pushed before printing with ,. When executing the '/ or \' in the wrong order after printing, it just does things to the stack (divides or swaps), and then pushes an irrelevant character before repeating.

MildlyMilquetoast

Posted 2012-12-07T16:21:03.537

Reputation: 2 907

I was just proposing a different 7 byte Befunge-98 solution on Jo King's answer. But this approach looks neater. – James Holderness – 2017-11-28T23:29:10.660

3

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.

histocrat

Posted 2012-12-07T16:21:03.537

Reputation: 20 600

3

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--;
  }
?>

hendra

Posted 2012-12-07T16:21:03.537

Reputation: 31

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 – 2012-12-13T05:14:37.607

3

ruby, 27 23 chars

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

$><< is 'print to stdout'.

steenslag

Posted 2012-12-07T16:21:03.537

Reputation: 2 070

2You should be able to replace "/\\" with '\/'. Ruby 1.8 requires [rand(2),1] to produce the character instead of the ascii code. – primo – 2012-12-18T10:22:57.610

3

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.

Brandorf

Posted 2012-12-07T16:21:03.537

Reputation: 131

3

Common Lisp - 68

(defun s(l)(elt l(random(length l))))(loop do(format t"~a"(s"/\\")))

Matthew Rolph

Posted 2012-12-07T16:21:03.537

Reputation: 131

Loop allows both a complex and a simple syntax, the simple one being like a progn, but looping forever. You can drop the do. – coredump – 2016-10-04T13:13:21.253

2

PHP, 26 31 bytes

eight three bytes shorter than yours (without whitespace or braces),
two bytes shorter than primo´s solution (without the tag)
.

PHP 5.4.9 was the current version in December 1012, so ...

for($s="\/";;)echo$s[rand()%2];

requires PHP 5.5 or later for literal string indexing.

Run with -r or try it online.

Titus

Posted 2012-12-07T16:21:03.537

Reputation: 13 814

PHP 5.5 didn't exist at the time of the challange posting, but have a +1 anyway. Also, try rand()&1 on a PHP build for Windows. You might be surprised. – primo – 2018-02-02T13:18:36.743

2

Cubix, 12 bytes

D<^<!"/\"B!o

View in the online interpreter!

This maps out to the following cube net:

    D <
    ^ <
! " / \ " B ! o
. . . . . . . .
    . .
    . .

Explanation

Setup:

The IP begins heading east at the first !. This is the 'skip if truthy' command, which is False when there is nothing on stack, so no commands are skipped. "/\" enters stringmode and appends these two character codes to the stack. B!o is mostly a no-op here, only reversing the stack.

The IP now loops back around to the first !. However, there are now positive integers on stack, so the first " is skipped. This means / is no longer a character, but a mirror, sending the IP north into the main loop.

Main Loop:

The D command is the only source of randomness in Cubix. It sends the IP in a random direction. By blocking off South and East with arrows, we make sure the IP has a 50% chance of heading North, and a 50% chance of heading West.

If it heads West, the stack is reversed. If it heads North, the top character is printed.

This creates the random sequence of slashes, as desired.

FlipTack

Posted 2012-12-07T16:21:03.537

Reputation: 13 242

2

SmileBASIC, 20 bytes

?"/\"[RND(2)];
EXEC.

12Me21

Posted 2012-12-07T16:21:03.537

Reputation: 6 110

2

JavaScript (Node.js), 59 55 54 bytes

Original Answer

while(1){process.stdout.write(Math.random()>=0.5?"/":"\\")}

-4 bytes thanks to manatwork

while(1)process.stdout.write(Math.random()<.5?"/":"\\")

-1 byte thanks to Conor O'Brien

for(;;)process.stdout.write(Math.random()<.5?"/":"\\")

1024x2

Posted 2012-12-07T16:21:03.537

Reputation: 21

Welcome to the site! – caird coinheringaahing – 2018-03-21T20:30:12.340

No need for braces around a single instruction block; in JavaScript the 0 integer part can be left out; reverse the conditional to use single character operator: while(1)process.stdout.write(Math.random()<.5?"/":"\\"). – manatwork – 2018-03-21T20:32:49.400

@manatwork Thanks! Updated. – 1024x2 – 2018-03-22T15:17:15.700

You can also write for(;;) instead of while(1) – Conor O'Brien – 2018-03-22T15:19:19.590

Thank you as well. Added. – 1024x2 – 2018-03-22T15:41:04.337

2

PowerShell, 108 90 86 54 53 37 36 bytes

for(){Write-Host "\/"[(Random 2)]-n}

Try it online!

facepalm42

Posted 2012-12-07T16:21:03.537

Reputation: 405

1

Welcome to Code Golf. Tips for golfing in PowerShell

– mazzy – 2019-07-20T09:40:48.363

2for also makes while a bit shorter – mazzy – 2019-07-20T10:53:21.617

1

you can more... more precisely, less :)

– mazzy – 2019-07-20T13:44:59.703

1

and less

– mazzy – 2019-07-20T15:55:56.153

2

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

while(1) { echo ['/', '\\'][rand(0, 1)]; }

rambo coder

Posted 2012-12-07T16:21:03.537

Reputation:

Never even thought of that, thanks! – Judy – 2012-12-07T16:29:42.787

1Actually PHP supports subscripts for strings: while(1)echo'/\\'[rand(0,1)];. – manatwork – 2016-10-04T13:06:45.503

2

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.

boothby

Posted 2012-12-07T16:21:03.537

Reputation: 9 038

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 – 2013-01-10T11:55:57.857

Python3: import random while 1:print(random.choice('/\'),end='') – Ivo – 2013-01-10T12:02:47.893

@Ivo, Thanks, I forgot about choice! You can keep py3, though. ;) – boothby – 2013-01-12T04:49:09.487

If you reverse your string, you do not have to escape the backslash, thus saving a byte. – Jonathan Frech – 2017-11-28T22:51:48.277

2

Java 8, 60 59 54 53 bytes

v->{for(;;)System.out.write(Math.random()<.5?47:92);}

-1 byte thanks to @BenjaminUrquhart by replacing print with write, so '/' can be 47.

Explanation:

Try it online (times out after 60 sec).

v->{                     // Method with empty unused parameter and no return-type
  for(;;)                //  Loop indefinitely
    System.out.write(    //   Print:
      Math.random()<.5?  //    If the random decimal value in range [0,1) is below 0.5:
       47                //     Print forward slash
      :                  //    Else:
       92);}             //     Print backward slash

Kevin Cruijssen

Posted 2012-12-07T16:21:03.537

Reputation: 67 575

1Java. 60. Bytes. Wow. – Erik the Outgolfer – 2016-10-04T13:50:20.737

@EriktheGolfer Hehe. I hadn't even noticed I'm about average in byte-count. Usually I'm just assuming Java is one of the highest byte-counts, only slightly competeable with BrainFuck or C# sometimes. xD (PS: I even golfed it to 59 :P) – Kevin Cruijssen – 2016-10-04T13:58:16.640

You are not average, you have a too small byte count (assuming Java, not that too small isn't good). But, at least, this isn't JAVA 8, where I think it's longer... – Erik the Outgolfer – 2016-10-04T14:00:38.793

@EriktheGolfer Hmm, isn't Java 8 shorter with c->{for(;;)System.out.print(Math.random()<.5?'/':92);} (54 bytes) Or did you mean Java 7 is longer than Java 8? In that case you are indeed correct. – Kevin Cruijssen – 2016-10-04T14:02:30.127

Dunno, I said that, usually, Java 8 is longer, because I have experienced seeing such bytecounts. Fr though, I haven't been able to take a Java tutorial for anything, because it simply does not work. – Erik the Outgolfer – 2016-10-04T14:09:12.240

153 bytes by switching to System.out.write and replacing '/' with 47 – Benjamin Urquhart – 2019-07-21T21:55:53.083

@BenjaminUrquhart Thanks! Need to remember that write when printing characters. o.Ô – Kevin Cruijssen – 2019-07-22T10:35:14.577

2

><>, 14 bytes

I was hoping I could restrict it to a 3*3 square but didn't succeed.

"/\
~x/
o</
 !

You can try it here.

Aaron

Posted 2012-12-07T16:21:03.537

Reputation: 3 689

1

16-bit x86 assembly code, 10 bytes

I don't remember if this one ended up in the book.

init:
scasb            ;read from where ES:DI points and compare to AL 
                 ;this sets flags similar to a subtraction 
salc             ;set mask in AL to 00 or FF 
and   al,'\'-'/' ;begin choosing character (AL is 00 or 2D) 
add   al,'/'     ;finish choosing character 
writec:
int   29h        ;write char in AL 
jmp   init       ;loop endlessly

peter ferrie

Posted 2012-12-07T16:21:03.537

Reputation: 804

1

Fission, 12 bytes

[#"\"R
W\"/"

Try it online!

KSmarts

Posted 2012-12-07T16:21:03.537

Reputation: 1 830

1

Perl 6, 23 bytes

loop {<\ />.pick.print}
  • loop loops forever.
  • <\ /> is a list of the strings \ and /.
  • .pick picks one of the two strings randomly.
  • .print prints that random string.

Sean

Posted 2012-12-07T16:21:03.537

Reputation: 4 136

instead of loop, you can use xx* – Jo King – 2019-07-20T13:16:59.647

1

Cubix, 10 bytes

Du\'^>o$/;

Watch it run

    D u
    \ '
^ > o $ / ; . .
. . . . . . . .
    . .
    . .

Was an interesting one to do as i haven't played with the random direction generator much.

  • ^D redirect to the random direction pointer

Directed north

  • o\> loops around the cube, print empty stack and redirecting back into the print commands (stack is empty)

Directed west

  • ^ redirected back into the random direction pointer

Directed south

  • \'/> reflected to the east, push / onto the stack and redirect into the print commands

Directed east

  • u'\> uturn to the right, push \ onto the stack and redirect into the print commands

Print commands

  • o$/;^ output top of stack, skip over the reflect, pop from the stack and redirect into the random direction pointer

MickyT

Posted 2012-12-07T16:21:03.537

Reputation: 11 735

1

Keg, 12 bytes

You do not have to worry about the evenness 0f the output, because the range is 0 to 32767, and 32767+1 = 32768; it is an even number.

{~2%[\\|\/],

user85052

Posted 2012-12-07T16:21:03.537

Reputation:

1

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();}

...

Naftali aka Neal

Posted 2012-12-07T16:21:03.537

Reputation: 129

stack space is not infinite. – codaddict – 2012-12-07T16:25:46.383

@codaddict so it will timeout. – Naftali aka Neal – 2012-12-07T16:26:54.780

Assuming a considerably high timeout your snippet is not equivalent to the OP's. – codaddict – 2012-12-07T16:31:25.880

1@codaddict lol the OPs times out as well. it is an infinite loop! – Naftali aka Neal – 2012-12-07T16:31:56.303

1Yours would create a run time error before the timeout. – codaddict – 2012-12-07T16:33:20.170

@codaddict no errors, just a timeout: http://codepad.org/nR6IunfF

– Naftali aka Neal – 2012-12-07T16:34:26.447

http://ideone.com/euCb5Z here you go SIGSEGV – codaddict – 2012-12-07T16:37:10.137

1

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);

Karoly Horvath

Posted 2012-12-07T16:21:03.537

Reputation:

1Due 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 – 2012-12-08T15:06:22.387

1

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)

Orabîg

Posted 2012-12-07T16:21:03.537

Reputation: 151

1

Powershell, 37 bytes

for(){Write-Host(Random("\","/"))-N}

unfortunately there seem to be no shorter aliases for Write-Host that don't cause a new line after every char.

whatever

Posted 2012-12-07T16:21:03.537

Reputation: 161

0

Tcl, 53 bytes

while 1 {puts -nonewline [expr rand()<.5?"/":"\\\\"]}

Try it online!

sergiol

Posted 2012-12-07T16:21:03.537

Reputation: 3 055

0

Pyth - 12 9 bytes

#@,\/\\O2

Tested without loop code because the online interpreter dislikes infinite loops

Explanation:

#@,\/\\O2
#             Repeat until Error
 @            Implicitly print item number
       O2     random number between zero and two exclusive
  ,           of two item list containing
   \/         One character string /
     \\       One character string \

Tornado547

Posted 2012-12-07T16:21:03.537

Reputation: 389

0

Add++, 24 bytes

+1
W,R2,-1,*45,+47,H,x:1

Try it online!

caird coinheringaahing

Posted 2012-12-07T16:21:03.537

Reputation: 13 702

0

q/kdb+, 18 bytes

Solution:

while[1;1"\\/"1?2]

Explanation:

A simple while(true) loop...

while[1;1"\\/"1?2] / the solution
while[1;         ] / while true do ...
              1?2  / 1 choose 2 (0 or 1)
         "\\/"     / string \/, index in at 0 or 1
        1          / print to stdout without newline

Extras:

Another 18 byte solution: {1"\\/"1?2}\[::;1]

streetster

Posted 2012-12-07T16:21:03.537

Reputation: 3 635

0

Yabasic, 28 bytes

Mid$("/\\",...) is used over Chr$() as the character codes for / and \ are 47 and 92, respectively.

While the GoTo structure of the original could easily be used for this answer, Do...Loop ends up being shorter due to the the structure of the Mid$ call.

Do?Mid$("/\\",Ran(3),1);Loop

Try it online!

Taylor Scott

Posted 2012-12-07T16:21:03.537

Reputation: 6 709

0

Japt, 9 bytes

iOo"/\\"ö

Try it

Shaggy

Posted 2012-12-07T16:21:03.537

Reputation: 24 623

0

MathGolf, 8 bytes

1Æû\/wq∟

Try it online!

Explanation

1          push 1
 Æ     ∟   do-while-true without popping using 5 operators
  û\/      push "\/"
     w     random char from string
      q    print without newline

Implicit popping doesn't seem to be working as intended when using do-while-true, or any other loop type. With this working correctly, the first 1 could be removed.

For a "working" 7-byter, you could do t{û\/wq, which replaces the do-while-true with a simple for loop, and begins the script by pushing the current timestamp in milliseconds to the stack. Currently, that means about \$1.56*10^{12}\$ iterations, which is "infinite" for most applications. However, this is not a valid answer, so the official answer is still 8 bytes.

maxb

Posted 2012-12-07T16:21:03.537

Reputation: 5 754

0

APL (Dyalog Unicode), 12 bytes

→≢⍞←'/\'[?2]

Try it online!

Tradfn body that prints indefinitely.

Thanks to @Adám for 5 bytes, and for pointing out that, with a little bit of CSS magic, TIO can print the actual pattern like in the image.

How:

→≢⍞←'/\'[?2] ⍝ Tradfn body
         ?2  ⍝ Roll (randomly select) an integer between 1 and 2
        [  ] ⍝ Use the result to index...
    '/\'     ⍝ ...the string containing the two slashes
  ⍞←         ⍝ Print it to the same line
 ≢           ⍝ Tally the result (yields 1)
→            ⍝ then 'goto' that line

J. Sallé

Posted 2012-12-07T16:21:03.537

Reputation: 3 233

This is eligible for 200 rep, no? – Adám – 2019-07-24T20:18:14.620

@Adám is it? I've no idea why it would. – J. Sallé – 2019-07-24T20:37:15.157

This. – Adám – 2019-07-24T20:38:44.380

1Oh, I see. I guess it is, then? There were no answers in any APL flavour before mine, and it meets all the conditions in the post. I'll add my request to it. – J. Sallé – 2019-07-24T20:49:38.047

Cool, and feel free to dig up any of your posts from since February. I have way too much rep. – Adám – 2019-07-24T20:51:21.880

0

Runic Enchantments, 13 bytes

`/-R`A*+k$!;

Try it online!

Slightly ungolfed for readability, with spaces to separate operational segments:

'/  2'RA  '- *+k $ !;

The difference of 45 is encoded in the - char literal, as chars are implicitly converted to integers when fed into a operator expecting a number value.

2'RA randomly generates a 0 or a 1, multiplied against the - and added to the / char resulting in either / or \, which is then printed after being coerced back to a char from an int. In the golfed version the (0x02) serves as the 2.

Then the program loops forever by skipping the terminator, !;.

Draco18s no longer trusts SE

Posted 2012-12-07T16:21:03.537

Reputation: 3 053

0

><>, 12 bytes

x!\"/!
^o< <

Try it online!

The idea here is to use x in combination with the ^ below to both loop and choose a random horizontal direction. If the IP goes left from the x, it will skip the mirror / and quote the string "\!x!/", reflect upwards with \, wrapping around to the <, which redirects the IP to output the result and begin the loop again. If the IP goes right from the x, a similar thing will happen.

This is all thanks to the nice happenstance that / and \ are control flow modifiers in ><>, allowing us to save a ".

An alternative solution for 13 bytes is slightly more generic and generates a lot fewer side effects.

xa!o"/\"o!a
^

(You can replace the as with spaces; I have them placed here for clarity. In production code, you would want to do this replacement to avoid unnecessary stack population.)

Conor O'Brien

Posted 2012-12-07T16:21:03.537

Reputation: 36 228

0

Python, 61 bytes

from random import*
while 1:print('/\\'[randint(0,1)],end='')

Pretty self explanatory. Had to use the double backslash since backslashes are escape characters in Python.

Try it Online!

EdgyNerd

Posted 2012-12-07T16:21:03.537

Reputation: 1 106

0

R, 33

Just interested to see whether this can be made smaller.

while(1)cat(sample(c('/','\\'),1))

Flounderer

Posted 2012-12-07T16:21:03.537

Reputation: 596

0

Clojure, 34 bytes

(loop[](pr(rand-nth"\\/"))(recur))

NikoNyrh

Posted 2012-12-07T16:21:03.537

Reputation: 2 361