From: VK on
Let be algorithm A that on each call returns pseudo-random number in
the range [0, 1[ thus it can be 0 sometimes but never 1

Let be algorithm B1 that takes each result from A and rounds it to
integer using method round() as described here:
http://msdn.microsoft.com/en-us/library/5cza0web%28VS.85%29.aspx
"For positive numbers, if the decimal portion of number is 0.5 or
greater, the return value is equal to the smallest integer greater
than number. If the decimal portion is less than 0.5, the return value
is the largest integer less than or equal to number."

Let be algorithm B2 that also takes each result from A and rounds it
to integer using method floor() as described here:
http://msdn.microsoft.com/en-us/library/sw6w4wz7%28v=VS.85%29.aspx
"The return value is an integer value equal to the greatest integer
less than or equal to its numeric argument."

Let {B1} and {B2} be finite sets of 0 and 1 obtained from algorithms
B1 and B2

Any platform-specific implementation issues aside: are there any
strictly mathematical reason why {B2} would be "more pseudo-
random" (better distributed) than {B1} ? The benefit of B2 over B1 is
claimed at
http://www.javascriptkit.com/javatutors/randomnum.shtml
without reasons given. For now I don't know if it's true or not and if
it is true than is it an implementational or algorithmical phenomenon.

From: A N Niel on
In article
<86447f1c-77d6-4d32-9698-293b63727f2e(a)q30g2000yqd.googlegroups.com>, VK
<schools_ring(a)yahoo.com> wrote:

> http://www.javascriptkit.com/javatutors/randomnum.shtml

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.
From: VK on
On May 1, 3:04 pm, A N Niel <ann...(a)nym.alias.net.invalid> wrote:
> Reply concerning this page, and not your description.

Yes, a nonsense I wrote due to nightly thinking, sorry. Already got
good for this in the original thread:
http://groups.google.com/group/comp.lang.javascript/msg/7e905faea33c9678

Based on all info the picture as I see it:
http://groups.google.com/group/comp.lang.javascript/msg/2f80c00a41d5d526
<quote>
Then would it be properly to state that in order to have the least
compromised pseudo-random sequence of integers from a set of two
elements one should use
return (Math.random() >= 0.5) ? _this : _that;
and for all sets with more than two elements one should use
return Math.floor( n * Math.random() );
where the range is [0, n-1]

Would it be appropriate to correct this in the FAQ
http://www.jibbering.com/faq/#randomNumber
and maybe add a short math explanation note based on the sci.math post
so not leaving reader to wonder why the hey floor() and what's wrong
with round() ?
</quote>



From: Tim Little on
On 2010-05-01, VK <schools_ring(a)yahoo.com> wrote:
> Let be algorithm A that on each call returns pseudo-random number in
> the range [0, 1[ thus it can be 0 sometimes but never 1
[...]
> Let be algorithm B2 that also takes each result from A and rounds it
> to integer using method floor()
[...]
> Any platform-specific implementation issues aside: are there any
> strictly mathematical reason why {B2} would be "more pseudo- random"
> (better distributed) than {B1}?

B2 always returns 0, so it is certainly not "more random" than B1.
However, in the context of the application you linked below it is
correct while B1 is not.


> The benefit of B2 over B1 is claimed at
> http://www.javascriptkit.com/javatutors/randomnum.shtml without
> reasons given.

The conclusion is correct, but the justification given on that page is
not. In particular, the clause "both successfully round off its
containing parameter to an integer within the designated range" is
erroneous: Math.round(Math.random()*11) will sometimes give the result
11, contradicting the requirement "the random number will fall between
0-10".


- Tim
From: William Hughes on
On May 2, 3:56 am, Tim Little <t...(a)little-possums.net> wrote:

> Math.round(Math.random()*11) will sometimes give the result
> 11,

Given that Math.random provides values from [0,1)
I do not see the "will".

I suppose that if Math.random produces the last floating
point number before 1, then the mathematical value of
Math.random*11 will be close enough to 11 that the floating
point value of 11 could be chosen. Is there something that
says it must be chosen.

- William Hughes