mardi 23 août 2016

How do I represent the smallest ever possibility in JavaScript?

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:

function true() {
  return Math.random() !== Math.random();
}

However, this runs into a few limitations.

  1. 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.
  2. 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.




Aucun commentaire:

Enregistrer un commentaire