Velocity Reviews > Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2)

# Math.random() and Math.round(Math.random()) and Math.floor(Math.random()*2)

John G Harris
Guest
Posts: n/a

 05-01-2010
On Sat, 1 May 2010 at 05:17:38, in comp.lang.javascript, VK wrote:

<snip>
> return (Math.random() >= 0.5) ? _this : _that;

<snip>
>Would it be appropriate to correct this in the FAQ

<snip>

Would beginners benefit from seeing a slightly faster routine for a
special case ?

John
--
John Harris

Lasse Reichstein Nielsen
Guest
Posts: n/a

 05-01-2010
VK <(E-Mail Removed)> writes:

> Assuming one needs to have a function returning false or true on each
> call in pseudo-random order.and using JavaScript native Math.random()
> method as the basis of the pseudo-randomness. Say the variants of such
> function are:
>
> var n = Math.round(Math.random());
> return n ? true : false;
> }
>
> var n = Math.floor(Math.random()*2);
> return (n==2) ? true : false;

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

> }
>
> Leaving obvious practical testing by platforms aside:
>
> Is there are theoretical considerations that pseudo-randomness
> (predictability) of either of above will be better or worse than the
> other one?

No, they are (obviously?) exactly identical. They map exactly the same
results of Math.random() to true and false respectively.
In both cases, a value in the range [0..0.5[ is mapped to false
and a value in the range [0.5..1[ is mapped to true.

> JavaScript Kit site claims that the second bits first:
> http://www.javascriptkit.com/javatutors/randomnum.shtml
> but they don't disclose the underlaying reasoning.

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

--
Lasse Reichstein Holst Nielsen
'Javascript frameworks is a disruptive technology'

Evertjan.
Guest
Posts: n/a

 05-01-2010
Lasse Reichstein Nielsen wrote on 01 mei 2010 in comp.lang.javascript:

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

or:

return !n-1;

--
Evertjan.
The Netherlands.

Ry Nohryb
Guest
Posts: n/a

 05-02-2010
On May 1, 11:20*pm, "Evertjan." <(E-Mail Removed)> wrote:
> Lasse Reichstein Nielsen wrote on 01 mei 2010 in comp.lang.javascript:
>
> > As stated elsewhere, this should read
> > * *return (n == 1) ? true : false;
> > or, preferably,
> > * *return n == 1;

>
> or:
>
> return !n-1;

return !n;
--
Jorge.

Dr J R Stockton
Guest
Posts: n/a

 05-02-2010
In comp.lang.javascript message <1b5852ed-217d-4b0a-8952-1212f96c5107@i9
g2000yqi.googlegroups.com>, Sat, 1 May 2010 00:54:03, VK
<(E-Mail Removed)> posted:
>Assuming one needs to have a function returning false or true on each
>call in pseudo-random order.and using JavaScript native Math.random()
>method as the basis of the pseudo-randomness. Say the variants of such
>function are:
>
> var n = Math.round(Math.random());
> return n ? true : false;
>}
>
> var n = Math.floor(Math.random()*2);
> return (n==2) ? true : false;
>}

That one always returns false.

>Leaving obvious practical testing by platforms aside:
>
>Is there are theoretical considerations that pseudo-randomness
>(predictability) of either of above will be better or worse than the
>other one? JavaScript Kit site claims that the second bits first:
> http://www.javascriptkit.com/javatutors/randomnum.shtml
>but they don't disclose the underlaying reasoning.

The cited page is basically incorrect; the author is probably rephrasing
something that he does not understand.

Only the incompetent, or those writing for them, feel a need to write
? true : false or ? false : true.

The proper answer is not to use Math.random >= 0.5 , but to use
Math.random < 0.5 - the result should be equally good, but the
latter takes fewer characters and out to be equally quick.

Implementations of Math.random should be pretty good at returning the
same number of results < 0.5 as not < 0.5 - but if there are any based
on N-bit shift registers with XOR feedback, then there should be with
those on average in every 2^N-1 results one more in one "half" than in
the other. That can, in practice, only be tested in JavaScript by
incipient struldbrugs.

One could use !Math.round(Math.random()) , but in principle one
should first check (in all browsers) that Math.round does not do
Bankers' Rounding. Anti-Bankers would be OK.

--
(c) John Stockton, nr London UK. ???@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Check boilerplate spelling -- error is a public sign of incompetence.
Never fully trust an article from a poster who gives no full real name.

Dr J R Stockton
Guest
Posts: n/a

 05-02-2010
In comp.lang.javascript message <(E-Mail Removed)>, Sat, 1 May
2010 21:30:44, Lasse Reichstein Nielsen <(E-Mail Removed)> 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

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;

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules