DEV Community

Discussion on: 10 Practical JavaScript Tricks

 
activenode profile image
David Lorenz

I am not sure what you are trying to say since it was about the uniformity and you questioned first the approach in itself since the 0.00000000000000001 is missing because 1 is never reached. So I tried simply telling you that 1 is reached and therefore the initial argument does not count since even though the exact number 0.99999999999999999 is mathematically not the same (as it does not have infiinite 9) it is in fact in JavaScript. But in fact 0.9repeating is the same as 1 mathematically en.wikipedia.org/wiki/0.999...

Then you were providing something as static 0.5 and then providing a solution that just does the same as what the author proposed but times 2 and therefore not really changing anything but the overall interval.

This feels like you are jumping into more and more topics also because you compare Maths with JS but JS only has a subset of what actual Maths provide (e.g. precision wise) so ACTUAL maths 1:1 compared to JS do not really fit since not all theories apply exactly the same.

When I was talking about "mistrust" I was talking about the fact that you mistrust the uniformity of the PRNG in Math.random because you say that the approach of the author shall not be used although I try to argue that for shuffling an array it is very much helpful and sufficient.

Also you could actually check the sourcecode of V8. If you are coding for browsers you can check WebKit, V8 and SpiderMonkey. For node.js you can do the same.

E.g. for WebKit trac.webkit.org/browser/webkit/

By checking those sourcecodes you could decide if they fit your - apparently weirdly rare - use-cases. Or in other words: What use-case are you trying to solve so maybe that helps better treating the problem that you apparently have.

So I am unsure what problem you are trying to solve right now or if you are just trying to rant to be honest.

If you want a better random generator then go for the WebCrypto API. I am out and will not longer respond to this weirdly side-escalating discussion.

Thread Thread
 
zachlitz profile image
Zach • Edited

First, this is all hypothetical. I already agreed that is doesn't matter.

But you are wrong in your premise. Assuming the PRNG was perfectly uniform, the fact that 1 is not included in the interval DOES AFFECT the shuffling. It doesn't matter that 0.999999 = 1.

Let's make the assumption that the PRNG is ideally uniform (otherwise nothing else matters).
Then let's make a list of every number that can be represented in JavaScript as an IEEE754 double precision FP number (which is what Math.random() returns)

The set is written in hex, but it starts like

0x0000000000000001 = 4.94065645841246544176568792868E-324

0x0000000000000002 = 9.88131291682493088353137585736E-324
...
0x3FEFFFFFFFFFFFFF = 9.99999999999999888977697537484E-1 (closest number to 1 before 1)
0x3FF0000000000000 = 1

set a = {
0x0000000000000001, 0x0000000000000002, 0x0000000000000003, 0x0000000000000004, ....
0x3FEFFFFFFFFFFFFF
}

set b = {
0x0000000000000001, 0x0000000000000002, 0x0000000000000003, 0x0000000000000004, ....
0x3FEFFFFFFFFFFFFF, 0x3FF0000000000000
}

Do you see how set a excludes 1, and set b includes 1?
Now split each set into two 1/2 sets where each half set as equal cardinality (if possible)

Do you agree that the two halves of set a are different than the two sets of set b?

Now, randomly pick any number from the entire sample space. Then choose the half set from a that contains the number, and choose the half set from b that contains the number. Guess what, you will pick the 1/2 set with the more numbers more times than the half set with the less numbers.

This is why it matters if 1 is included. You keep saying that 0.99999999=1 but that doesn't mean anything. In fact, it less than meaningless because all you are saying is the decimal value 0.99999999 is represented as 1 as a 64-bit FP number. You are conflating the decimal representation with the in-memory representation. We are dealing with finite sets of discrete numbers.

My original argument was that sorting using the described method will result in a biased sort.
I have since said that it doesn't matter because even assuming the PRNG was perfectly uniform the bias would be insignificantly small, and even more so, it doesn't matter because the PRNG is a black box that is most likely far from perfectly uniform.

But my original statement is correct. Excluding 1 changes the behavior, unless that change is accounted for.

And you are most likely correct that my solution was not an improvement. The first one was 100% wrong, the second might not do anything because it still has the built flaw of not including 1 in the original random number. But my point wasn't to solve the problem, just to show that it won't result in a uniform sort.