Objective
The title of Big Happy Brother is finished, but a new problem is derived! In the last essay, my brother and I used the random number generation method, in the end who is more fair???
Body
Welcome to a section of the blog "ideas"! Our slogan is "How big the Heart, the bug is how big"!
Here are some of our guests:
Small Peng brother, Peng Brother from XXXxxx, get xxxxxx (a pile of titles). Front-end old driver. We applaud and welcome our small Peng, Papapa .... (Peng elder brother no big Huan elder brother tall, but small Peng elder brother has a tall heart, can be seen from the car).
Big Huan elder brother, Big Huan elder brother Front introduction, now do not introduce (big Huan elder brother at this time in the heart has a "I go, not introduce me", hey hey hehe ......, a joke), our Big Brother from xxxxxxxx, get xxxxxx (a pile of titles). Java old driver. We applaud and welcome our Big Brother, Papapa .... (Big Huan elder brother tall and mighty, but the heart is soft, the old ritual still is a car to see out).
Topic: Title (Hey hehe ...) )
Topic Code:
1 var ran1 = Math.floor (Math.random () *a.length); // Way One 2 3 var ran2 = Math.Round (Math.random () * (a.length-1)); // Mode two
Note: It is assumed that the random number generated by Math.random () in JavaScript is between 0-1 and is absolutely fair. A is an array of length 4.
Code statement:
Mode one: Generates a random number between 0 and 1 (assuming absolute fairness). The random number is multiplied by the length of a, and the final rounding is taken . Big Huanhuan Code!
Mode two: Generates a random number between 0 and 1 (assuming absolute fairness). A random number is multiplied by the length of a minus one, and the final rounding takes an integer . My code, small Peng Brother my argument.
Big Huan elder brother: host, not ah, people's length is 4, why do you want to subtract a ah, this to others array a the last element is unfair!
Brother Peng: Gee, oops. I'll go. Listen to him like that, maybe, maybe it's a little unfair .... (In thought)!
Good! Two guests all feel that the way two is not fair, then two guests to argue about the way two why not fair! Two guests on the sleeves, take the pen and paper ...
.
.
.
.
(Forgive me for the limited eloquence, speak directly the results of the argument!) )
Logical concept diagram:
Look at the graph thinking:
The way an array of a is abstracted into four parts! The spacing of each part is the same (one, two, three, four)! When the random number produced by Math.random () can be uniformly dropped in four intervals (distance equidistant) each interval is 25% , so the random generated random number only in the Math.random function assuming fairness, the way a random number generated, it is fair!
Conclusion:
Value |
corresponding interval |
Probability |
0 |
First interval |
25% |
1 |
Second interval |
25% |
2 |
Third Zone |
25% |
3 |
Zone Four |
25% |
Mode two divides an array of a into three parts (length-1)! The spacing of each part is equal (One-third: 33.333333%), but the probability of falling in the first interval (0~0.5) is only 33.3333333333.........%/2= after rounding the Math.Round (). 16.666666666.......%! The same probability of falling in the fourth interval is also 33.3333333333.....%/ 2=16.66666........%, that is, assuming math.random () is absolutely fair, the probability of getting 0 or 3 is only 16.6666666.....% , and 2 and 3 are up to 33.3333. 33333......%.
Conclusion:
Value |
corresponding interval |
Probability |
0 |
First interval |
16.6666666666% |
1 |
Second interval |
33.3333333333% |
2 |
Third Zone |
33.3333333333% |
3 |
Zone Four |
16.6666666666% |
Thank you Big Huan elder brother and small Peng Brother's wonderful argument, no wonder you did not draw the award at the annual meeting. It turns out you used the wrong API. This is the end of this issue of a blog, "The idea of ideas." Thank you! See you next time ...
Conclusion
Combined with the above argument, the Math.random () is absolutely fair. Way one way two absolutely fair!!!
For the above results, only relative to the theoretical verification, no actual data validation, if there are different views, welcome to point!!!
On the relative fairness of JavaScript taking random number in the idea of strange ideas!!!