From: VK on
On May 1, 4:02 pm, "Richard Cornford" <Rich...(a)litotes.demon.co.uk>
wrote:
> >... 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. ?
>
> The issue only applies to rages with more than two values in them. But
> still, Jorge's remains the better implementation for javascript (FPU
> handled math operation over a method call), and it uses neither
> Math.round nor Math.floor.

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() ?


From: Evertjan. on
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.
(Please change the x'es to dots in my emailaddress)
From: Ry Nohryb on
On May 1, 11:20 pm, "Evertjan." <exjxw.hannivo...(a)interxnl.net> 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.
From: Johannes Baagoe on
VK :

> Shannon's Clairvoyant can quickly tell for any sequence is it's
> truly random or pseudo-random.

Of course not. It is quite difficult to define "random" and "pseudo-
random" in a precise and satisfactory way, but an essential part of
any reasonable definition is that there is no quick way to tell the
difference given the sequences. What you call "Shannon's Clairvoyant"
(I don't know it by that name from any other source than you) is a
rather clever illustration of the shortcomings of *humans* as sources
of random or pseudo-random sequences, nothing more. Computers are
much better, and easily defeat your "clairvoyant".

> btw Wiki's http://en.wikipedia.org/wiki/Pseudorandom_function_family
> claim that "No efficient algorithm can distinguish (with significant
> advantage) between a function chosen randomly from the PRF family"
> is a complete b.s.

No, it is a sensible definition.

> but I am too lazy to argue with the entire ACM.

Perhaps some lingering remains of sanity, as well, like when you
chickened out of a bet you proposed yourself on the subject. Some
part of you may dimly realise that some people actually know more
about the subject than you.

Still, I am curious to see your implementation of "Shannon's
Clairvoyant" when you finish it, even if does not allow me to win
$10,000 against the loss of $10 according to the flip of a coin.

--
Johannes
From: Dr J R Stockton on
In comp.lang.javascript message <1b5852ed-217d-4b0a-8952-1212f96c5107(a)i9
g2000yqi.googlegroups.com>, Sat, 1 May 2010 00:54:03, VK
<schools_ring(a)yahoo.com> 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:
>
>getAnswer1() {
> var n = Math.round(Math.random());
> return n ? true : false;
>}
>
>getAnswer2() {
> 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.