3 ms·
Shorter version: chanceOfHead=0.9; heads=0; tails=0; flips=0; while(flips<1000) { coin1=Math.random()<chanceOfHead; coin2=Mat
by TekMol 9y ago
Shorter version:
chanceOfHead=0.9;
heads=0;
tails=0;
flips=0;
while(flips<1000)
{
coin1=Math.random()<chanceOfHead;
coin2=Math.random()<chanceOfHead;
if(coin1==coin2) continue;
heads+=coin1;
tails+=coin2;
flips++;
};
console.log(heads,tails);
- oh_sigh 9y agoOh JavaScript. Will a Boolean always on all browsers coerce itself into a 0 or 1 if treated like a number?
- tjgq 9y agoThe Javascript coercion rules, arcane as they are, are actually part of the spec and (barring any bugs) should work across browsers.
- twodave 9y agoSure, that will get the basics. We wanted to compare it to what an unbiased set of flips of the same number would look like over time, so there's a little more to it. It's also interesting (and I guess should be obvious) to note that the "thrown out" tosses seem to be a pretty consistent ratio based on the "biased-ness" of the coin.