From: Dr J R Stockton on
In comp.lang.javascript message <ocgz5sij.fsf(a)gmail.com>, Sat, 1 May
2010 21:30:44, Lasse Reichstein Nielsen <lrn.unread(a)gmail.com> posted:

>As stated elsewhere, this should read
> return (n == 1) ? true : false;
>or, preferably,
> return n == 1;

Or return ! Math.floor(Math.random()*2);

That gives the opposite answer, but who cares?

return Math.random()<0.5 should be quicker.



>I guess their point is that to generate an integer in the range [0..n[,
> Math.floor(Math.random() * n)
>is better, in general, than
> Math.round(Math.random() * (n - 1))
>... which is pretty old news (not that people still don't bungle it
>regularly, but it's embarrasing every time it happens).
>
>The funny thing is that for n = 2, the unevenness of using the Math.round
>doesn't matter, which is the case you are asking about.

Since using Math.round makes the two end bins each half as probable as
the rest, n = 1 and n = 1 are the only cases where it gives equi-
probability.

However, there is a class of problems where Math.round is good : "a
metre stick is repeatedly put at random positions within a ten-metre
tube marked out in one-metre sections - what is the probability
distribution of which section its centre is in?"
(for the metrically-challenged : use .replace(/re\b/g, "er") on that)

--
(c) John Stockton, nr London, UK. ?@merlyn.demon.co.uk Turnpike v6.05 IE 7.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
MiniTrue is good for viewing/searching/altering files, at a DOS / CMD prompt;
free, DOS/Win/UNIX, new via <URL:http://www.merlyn.demon.co.uk/pc-links.htm>.