random - How do I represent the smallest ever possibility in JavaScript? -
i want design function return true
of time theoretically return false
.
so far, i've come (with comments added, due confusion):
function true(seed) { // poop, can't `seed` math.random()! return math.random() !== math.random(); } // if had seed, , math.random() seedable, // make function return false.
however, runs few limitations.
math.random()
implementation not seedable therefore calling random number generator twice in row (with no other entropy) never return same number twice.math.random()
return value between 0.0000000000000000 , 0.9999999999999999, sixteen digits of precision. according binomial distribution probability of true not being true (1/9999999999999999)^2. or 1.0 e-32.
what trying build return false
in probability of 1/some integer grows larger , larger. purely thought experiment. there no constraint on space , time, although if answer has considered that's bonus.
edit: guess, here way ask question.
take @ 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 expected console.log(f(math.random())); // pretty returns false function t(seed) { math.seedrandom(seed); return 0.7781282080210712 !== math.random(); } console.log(t()); // returns true. console.log(t(math.random())); // world. // but, if have right seed! console.log(f('udia')); // holy shit, returned true! console.log(t('udia')); // holy shit, returned false!
what interesting way write function returns true
? can run forever, take space possible, etc. must return true
. (and have smallest probability of returning false
.)
since math.random()
not yield same number twice in row, this:
var improbabilitydrive = math.random(); var discard = math.random(); function true() { return math.random() !== improbabilitydrive; }
or, if don't want global variables, discard next few results:
function true() { var improbabilitydrive = math.random(); var discard = math.random(); discard = math.random(); discard = math.random(); //... more discards, if necessary return math.random() !== improbabilitydrive; }
edit: drop probability each time it's called
op asked if it's possible make less , less return (false, think meant?)
var hitsrequired = 0.0; var improbabilitydrive = math.random(); //increasingly lower chance of 'false' each call function supertrue() { hitsrequired += 0.1; //set growth factor here (algebraic: +=, geometric: *=) (int = 0; < hitsrequired; i++) { if (trueish()) return true; } return false; } //same theoretically low chance of 'false' each call function trueish() { var discard = math.random(); discard = math.random(); discard = math.random(); //... more discards, if necessary return math.random() !== improbabilitydrive; }
edit 2: insanely low probability
after re-reading question, think you're after most-low probability can get. far, far, below reason:
//increasingly lower chance of 'false' each call function superdupertrue() { (int = 0; <= 9007199254740992; i++) { if (trueish()) return true; } return false; }
the probability produced false is:
(1/4503599627370496) ^ 9007199254740992 = 10 ^ ( - 10 ^ 17.15)
that would, reasonable measure, such absurdly low probability assumed never happen. i'd surprised if return single false if tried trillion times per second until heat death of universe - putting wolfram alpha didn't drop number of probability (1 trillion * 10^100 years until heat death of universe * 3,156,000 seconds / year * probability = probability, subject 14 decimal places of accuracy).
basically, never happen, theoretically possible.
at 1,000,000,000,000 tries per second:
for n=0, 38 minutes yeild 50% chance of single false.
for n=1, 325 billion years yeild 50% chance of single false.
for n=2, 1500000000000000000000000000 years (1.5 * 10^17), or 110000000000000000 times age of universe yeild 50% chance of single false.
... increase n 9007199254740992, above, make implausible desire.
Comments
Post a Comment