I want to design a function that would return true
most of the time but theoretically could return false
.
So far, all I've come up with is (with comments added, due to some confusion):
function true(seed) {
// Poop, you can't `seed` Math.random()!
return Math.random() !== Math.random();
}
// but if I had that seed, and Math.random() was seedable,
// I could make this function return false.
However, this runs into a few limitations.
Math.random()
by implementation is not seedable therefore calling a random number generator twice in a row (with no other entropy) will never return the same number twice.Math.random()
will return a value between 0.0000000000000000 and 0.9999999999999999, which is sixteen digits of precision. So according to Binomial Distribution the probability of true not being true is (1/9999999999999999)^2. Or 1.0 e-32.
What I am trying to build is something that would only return false
in the probability of 1/some integer that grows larger and larger. This is purely a thought experiment. There is no constraint on space and time, although if your answer has considered that as well then that's a bonus.
EDIT: I guess, here is another way to ask this question.
Take a look at this Plunker. https://plnkr.co/edit/C8lTSy1fWrbXRCR9i1zY?p=preview
<script src="//cdnjs.cloudflare.com/ajax/libs/seedrandom/2.4.0/seedrandom.min.js"></script>
function f(seed) {
Math.seedrandom(seed);
return 0.7781282080210712 === Math.random();
}
console.log(f()); // Behaves as expected
console.log(f(Math.random())); // Pretty much everything returns false
function t(seed) {
Math.seedrandom(seed);
return 0.7781282080210712 !== Math.random();
}
console.log(t()); // Returns true.
console.log(t(Math.random())); // All is well with the world.
// But, if you have the right seed!
console.log(f('Udia')); // Holy shit, this returned true!
console.log(t('Udia')); // Holy shit, this returned false!
What is the most interesting way to write a function that returns true
? It can run forever, take up as much space as possible, etc. But it must return true
. (and have the smallest probability of returning false
.)