On May 1, 3:16*pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
> You leave practical testing aside far to often in your posted code. Any
> reasonable testing (or your just understanding the methods/operations
> employed) would observe that your proposed - getAnswer2 - function only
> ever returns false. Thus, it fails to satisfy your "returning false or
> true on each call in pseudo-random order".
Yeah... My mind was distracted a lot by the binary trees observations,
sorry.
> > Is there are theoretical considerations that pseudo-randomness
> > (predictability) of either of above will be better or worse than
> > the other one?
>
> Yes, but mostly because the obvious bugs in the second prevent it from
> doing anything useful at all.
>
> > 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 subject of the comments on that page is the choice of the use of
> Math.floor over Math.round (where Math.round is commonly used in example
> javascript random number generators found on the Internet).
>
> Math.random returns a (pseudo-random) number that is in the range zero
> to less than one (i.e. anything non-negative that is smaller than one).
> If you multiply that number by an integer you will get a result that is
> in the range from zero to less than that number. If two were taken as an
> example of such a number (i.e. - (Math.random * 2) - the result would be
> a number in the range zero to less than two.
>
> If you apply - Math.round - to all the numbers in the range zero to less
> than two the range zero to <0.5 (a quarter of the total range) would
> result in zero, between 0.5 and <1.5 (half the total range) would result
> in one, and 1.5 to <2 would result in two. So, using Math.round, a
> random number in the 0 to <2 range has a 25% chance of coming our zero,
> a 50% chance of coming out one and a 25% chance of coming out 2. This is
> not an even distribution. (Extending this to multiplying by any positive
> integer; it is the values at the two extremes of output that end up
> being half as likely in the output as any numbers in between, however if
> that integer were one then there would be no numbers in between the
> extremes of the range and so then the distribution between zero and one
> would be equal.)
>
> If you apply - Math.floor - to numbers in the range 0 to <2 then the
> range 0 to <1 (half the original range) result in 0 and the range 1 to
> <2 (the other half of the original range) result in 1; a 50/50
> distribution.
Right. Similar answer from sci.math :
http://groups.google.com/group/sci.m...878f7a0aea0b90
<quote>
Reply concerning this page, and not your description.
Let X be uniformly distributed in [0,1). Then floor(X*11) takes
values
0,1,...,10 each with probability 1/11 ... that is what the page means
by "even". However round(X*10) takes value 0 with probability 1/20,
values 1,...,9 each with probability 1/10 and value 10 with
probability
1/20. Not "even" according to the page.
</quote>
So the question is then
a) if it is possible to use Math.floor for a pseudo-random input to
get binary output (1/0, true/false)
b) if so than will it be more even probability for output than for
Math.round(Math.random()) or (Math.random >= 0.5)
.... or Math.floor(Math.random()*N) benefits appear only for ternary
and wider ranges "0 or 1 or 2", "0 or 1 or 2 or 3" etc. ?