15
\$\begingroup\$

Create a program that halts exactly 50% of the time. Be original. Highest voted answer wins. By exactly I mean that on each run there is a 50% chance of it halting.

\$\endgroup\$
17
  • 9
    \$\begingroup\$ I mean that it should have an exactly 50% probability to halt on every run. \$\endgroup\$
    – ike
    Jan 1, 2014 at 12:41
  • 5
    \$\begingroup\$ If the program doesn't halt, does that mean it runs forever? It'll sure as hell halt when I turn the PC off. (Unless it is NSA code, then who knows...) \$\endgroup\$
    – Paul
    Jan 1, 2014 at 12:44
  • 8
    \$\begingroup\$ Who keeps upvoting these poor questions? \$\endgroup\$
    – Gareth
    Jan 2, 2014 at 11:04
  • 6
    \$\begingroup\$ This is a fine question. Only those who don't understand probability are confused by it. The original title was perhaps a bit misleading, but no worse than the New York Times. \$\endgroup\$ Jan 3, 2014 at 4:45
  • 4
    \$\begingroup\$ I found it perfectly clear. Create a program that has a 50% chance of halting (or, equivalently a 50% chance of falling into an infinite loop), and you cannot know which will occur before every runtime. \$\endgroup\$
    – ejrb
    Jan 3, 2014 at 9:25

42 Answers 42

1
2
0
\$\begingroup\$

Exactly 50% of the time?

OBJ-C

- (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
    BOOL haltedLastRun = [(NSNumber*)[[NSUserDefaults standardUserDefaults] objectForKey:@"halted"] boolValue];
    if (!haltedLastRun) {
        [[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithBool:YES] forKey:@"halted"];
        [[NSApplication sharedApplication] terminate:nil];
    }
}
\$\endgroup\$
0
\$\begingroup\$

Haskell

Runs for two intervals, each one 1 second long (chosen because 1 second is the SI unit for time). Halts inside 50% of the intervals. So 50% of the running seconds it will not halt, the other 50% it will. Works in GHC only.

import Control.Concurrent (threadDelay)
main = threadDelay 1990000
\$\endgroup\$
0
\$\begingroup\$

Shell Script

this script will clobber .md5sum files in the current and child directories.

#!/bin/sh
echo *.md5sum|xargs -n1|head -n1|xargs test -e && exec rm *.md5sum
while ! find . -name '*.md5sum' -print0 |xargs -0r grep 00000000000000
do {
    find . -type f -print|sed -e 's!^\(.*\)$!md5sum "\1" > "\1".md5sum!e'
}
done
\$\endgroup\$
0
\$\begingroup\$

GTB

[@r;p;]

I know this isn't code-golf but I decided to golf it anyway.

\$\endgroup\$
0
\$\begingroup\$

C++

#include <fstream>
main () {
    int c;
    std::fstream fs;
    fs.open ("myfile.txt", std::fstream::in);
    fs>>c;
    fs.close ();
    fs.open ("myfile.txt", std::fstream::out);
    fs<<c+1;
    fs.close ();
    while (c%2);
    return 0;
}

Each run will halt iff the run before didn't.

\$\endgroup\$
0
\$\begingroup\$

Windows Command Script

This script will append code to itself which ultimately alternates 'x' on each run.

call :last
if %x%==1 (
    echo>>%0 set x=0
    exit /b 0
) else (
    echo>>%0 set x=1
)
:nohalt
goto :nohalt
:last
set x=1
[newline here]
\$\endgroup\$
0
\$\begingroup\$

Math++

_($rand*2)+2>$
2>$
1
\$\endgroup\$
0
\$\begingroup\$

Python 2, 54 bytes

import time;H=[time.time()%2]
for h in H:H+=[h]*int(h)

Try it online!

Halting behaviour is dependent on current time.

\$\endgroup\$
0
\$\begingroup\$

Pxem, filename: 7 bytes + content: 0 bytes = 7 bytes, depends on implementation.

  • Filename (escaped): \002.r.w.a
  • Content: empty.

How it works

  • \002.r: pushes one of 0 or 1.
  • .w ... .a: while empty || pop!=0; do ... ;done

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Thue, 19 bytes

0::=00
10::=
::=
10

Try it online!

TIO's Thue interpreter doesn't seem to be working correctly.

\$\endgroup\$
0
\$\begingroup\$

Thunno, \$ 6 \log_{256}(96) \approx \$ 4.94 bytes

2RZw?[

Attempt This Online!

Explanation

2R      # range(2): push [0, 1]
  Zw    # Random element of ↑
    ?   # If ↑ == 1:
     [  #  Loop forever
        # (Else, exit)
\$\endgroup\$
0
\$\begingroup\$

C (GCC)

unsigned long long i,j,k;
r;

main()
{
    srand(time(0));

    i=k=1;
    while (k) {
        k = 0;
        for (j = 1; j <= i; ++j) {
            do {
                r = rand() % 4;
            } while (r == 3);
            
            k += !!r;
        }
        ++i;
    }
}

Try It Online!

It loops while k isn't 0. k is only 0 at the end of the loop if i number of calls to rand() are all divisible by 3. The chance of that equals

$$ \frac {1} {3^i} $$

and the sum of all inverse natural powers of 3 equals 0.5. Of course, i doesn't go from 1 to infinity, but from 1 to ULLONG_MAX. When i reaches ULLONG_MAX, the for loop will loop infinitely as j will always be less than or equal to i (assuming ULL_MAX + 1 is 0).

Since i doesn't quite loop from 1 to infinity, the code will loop slightly less than 50 % of the time, more specifically

$$ 50 \% - \frac {1} {3^{U+1} - 3^{U}} $$

where U is ULLONG_MAX.

\$\endgroup\$
2
  • \$\begingroup\$ there's also the problem with rand() % 3 not being uniformly distributed, which leads to the probability of it being 0 to slightly increase \$\endgroup\$
    – c--
    Feb 15, 2023 at 16:07
  • \$\begingroup\$ @c-- True … I'll see what I can do about that. \$\endgroup\$
    – Peter
    Feb 15, 2023 at 18:54
1
2

Your Answer

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

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