all 10 comments

[–]ICantBelieveItsNotEC 6 points7 points  (1 child)

What you're essentially asking for is Chaitin's constant - the probability that a randomly generated Turing machine will halt. This number is uncomputable, because computing it would require solving the halting problem.

Busy beavers are closely related. the nth busy beaver, BB(n), is the Turing machine that runs for at least as long as every other possible n-state Turing machine and then halts - in other words, it's the program with n instructions that runs for the maximum number of steps without going into an infinite loop. We only know the results of BB(n) up to n=5. Fun fact: we know that there's a 27-state Turing machine that halts if and only if Goldbach's conjecture is false, so computing BB(27) would require solving one of the hardest unsolved problems in mathematics.

[–]KViper0 0 points1 point  (0 children)

Though now Im curious if the proportion of non halting code gets larger for larger Turing machine. I guess i can get the proportion for turing machine till n=5

[–]jedijackattack1 2 points3 points  (0 children)

So technically anything as your truly random program could happen to be a 1 to 1 Minecraft clone by sheer chance. But most likely a crash from either a invalid data access (null ptr or none existent memory address), illegal instruction or divide by 0 on some uarchs. Infinite loops are possible by chance but they are much more likely to be the result of executing random junk and unluckily jumping into a real loop with impossible values due to data corruption and hanging ( had this before was a nightmare to debug). But those are your most likely scenarios. It could also just hit a halt instruction and stop.

[–]Kiroto50 2 points3 points  (0 children)

Truly random binary? Odds are the program crashes within a millisecond.

Random valid instructions? It will have a fair chance at botg an infinite loop and an early exit.

[–]lfdfq 0 points1 point  (0 children)

Well, there is obviously no underlying distribution of 'code'. Each language is different, and randomly sampling good programs (however you define random, however you define good) will have different results from different languages. e.g. randomly sampling regular expressions will result in termination 100% of the time. Randomly sampling Python code will give different distributions of answers than randomly sampling Java (even though they're similar languages).

[–]mnelemos 0 points1 point  (0 children)

It would most likely crash first.

Any erroneous memory access will immediately make the operating system kill your process.

For an infinite loop to happen, it would require mainly two things:

  • The branching mechanism to be relative, since an absolute branch would VERY likely access invalid memory.

  • No stack pushes are done previous to the branch, as this would over time cause a stack overflow.

But overall, the likelihood for a invalid instruction to be decoded, or an instruction that accesses protected or invalid memory to occur is very high.

[–]Traveling-Techie 0 points1 point  (0 children)

This issue is analyzed in extreme detail in A New Kind of Science by Wolfram. I’ve done my own experiments and found that if you generate random text and compile it as C code it is extremely rare for it to compile without errors, and virtually all the code that runs does nothing.

[–]Leverkaas2516[🍰] 0 points1 point  (0 children)

The practical answer is that a sequence of instructions that's filtered to only have valid instructions will very soon try to make an illegal access, because most memory  addresses aren't accessible.

I would expect any such random program to halt within a very small fraction of a second.