Code Monkey home page Code Monkey logo

dice-fairness's Introduction

Dice Fairness

A little Deno application that tells you to stop throwing your dice to test whether they are fair.


It simulates throwing dice until each side is within some margin of the expected value.

For a six-sided die the expected percentage of every side would be 16.66%. A simulation generates random rolls until the percentage of every side is within 0.1666 ± (0.1666 × p), where p is the allowed margin.

If the first six rolls are all different it stops, as the die would appear to be perfectly fair. Of course this will only happen in rare cases and the average over a number of simulations gives a better idea of what kind of numbers to expect.

Arguments

-n <number>    Number of simulations - Default: 100
-d <number>    Number of sides of the die - Default: 6
-p <number>    Margins, in percent, around the expected probability that
               have to be met for all sides of the die to stop a simulation.
               Default: 5
-q             Quiet mode. Does not print the results of the individual simulations.

Example Results

deno run ./src/main.ts -q -n=10000 -d=4

4-sided die
Number of simulations: 10000
Average number of throws to be within 5%: 847.0721
Max: 12789 | Min: 4

deno run ./src/main.ts -q -n=10000 -d=6

6-sided die
Number of simulations: 10000
Average number of throws to be within 5%: 2990.5371
Max: 28844 | Min: 6

deno run ./src/main.ts -q -n=10000 -d=8

8-sided die
Number of simulations: 10000
Average number of throws to be within 5%: 5621.4179
Max: 41532 | Min: 8

deno run ./src/main.ts -q -n=10000 -d=10

10-sided die
Number of simulations: 10000
Average number of throws to be within 5%: 8687.0361
Max: 49970 | Min: 10

deno run ./src/main.ts -q -n=10000 -d=12

12-sided die
Number of simulations: 10000
Average number of throws to be within 5%: 12074.2283
Max: 76309 | Min: 12

deno run ./src/main.ts -q -n=10000 -d=20

20-sided die
Number of simulations: 10000
Average number of throws to be within 5%: 27716.3072
Max: 124439 | Min: 4858

Example without quiet:

deno run ./src/main.ts -n=100 -d=6
Output

Throws: { "1": 2157, "2": 2054, "3": 2093, "4": 2142, "5": 2228, "6": 2058 }
Total #: 12732
Throws: { "1": 252, "2": 267, "3": 256, "4": 273, "5": 249, "6": 263 }
Total #: 1560
Throws: { "1": 63, "2": 66, "3": 69, "4": 64, "5": 68, "6": 66 }
Total #: 396
Throws: { "1": 628, "2": 676, "3": 667, "4": 678, "5": 664, "6": 651 }
Total #: 3964
Throws: { "1": 68, "2": 71, "3": 74, "4": 70, "5": 67, "6": 73 }
Total #: 423
Throws: { "1": 969, "2": 986, "3": 893, "4": 949, "5": 925, "6": 918 }
Total #: 5640
Throws: { "1": 1283, "2": 1225, "3": 1271, "4": 1343, "5": 1294, "6": 1259 }
Total #: 7675
Throws: { "1": 85, "2": 90, "3": 84, "4": 90, "5": 85, "6": 92 }
Total #: 526
Throws: { "1": 33, "2": 32, "3": 35, "4": 33, "5": 32, "6": 35 }
Total #: 200
Throws: { "1": 147, "2": 145, "3": 150, "4": 155, "5": 147, "6": 142 }
Total #: 886
Throws: { "1": 33, "2": 30, "3": 33, "4": 31, "5": 32, "6": 30 }
Total #: 189
Throws: { "1": 364, "2": 372, "3": 343, "4": 343, "5": 369, "6": 375 }
Total #: 2166
Throws: { "1": 110, "2": 115, "3": 114, "4": 105, "5": 109, "6": 105 }
Total #: 658
Throws: { "1": 715, "2": 690, "3": 724, "4": 666, "5": 670, "6": 735 }
Total #: 4200
Throws: { "1": 154, "2": 159, "3": 148, "4": 161, "5": 146, "6": 153 }
Total #: 921
Throws: { "1": 1145, "2": 1099, "3": 1061, "4": 1136, "5": 1047, "6": 1055 }
Total #: 6543
Throws: { "1": 228, "2": 241, "3": 224, "4": 246, "5": 234, "6": 233 }
Total #: 1406
Throws: { "1": 124, "2": 123, "3": 130, "4": 134, "5": 134, "6": 131 }
Total #: 776
Throws: { "1": 247, "2": 266, "3": 256, "4": 266, "5": 271, "6": 254 }
Total #: 1560
Throws: { "1": 347, "2": 362, "3": 345, "4": 378, "5": 370, "6": 358 }
Total #: 2160
Throws: { "1": 384, "2": 349, "3": 363, "4": 382, "5": 361, "6": 365 }
Total #: 2204
Throws: { "1": 301, "2": 276, "3": 296, "4": 274, "5": 297, "6": 284 }
Total #: 1728
Throws: { "1": 255, "2": 258, "3": 266, "4": 259, "5": 254, "6": 274 }
Total #: 1566
Throws: { "1": 346, "2": 336, "3": 340, "4": 364, "5": 335, "6": 365 }
Total #: 2086
Throws: { "1": 449, "2": 410, "3": 441, "4": 409, "5": 446, "6": 428 }
Total #: 2583
Throws: { "1": 115, "2": 115, "3": 105, "4": 109, "5": 107, "6": 107 }
Total #: 658
Throws: { "1": 119, "2": 118, "3": 110, "4": 110, "5": 113, "6": 120 }
Total #: 690
Throws: { "1": 623, "2": 576, "3": 575, "4": 608, "5": 623, "6": 622 }
Total #: 3627
Throws: { "1": 439, "2": 436, "3": 466, "4": 466, "5": 469, "6": 477 }
Total #: 2753
Throws: { "1": 264, "2": 258, "3": 266, "4": 276, "5": 254, "6": 260 }
Total #: 1578
Throws: { "1": 139, "2": 139, "3": 149, "4": 141, "5": 140, "6": 144 }
Total #: 852
Throws: { "1": 649, "2": 691, "3": 703, "4": 714, "5": 654, "6": 687 }
Total #: 4098
Throws: { "1": 4, "2": 4, "3": 4, "4": 4, "5": 4, "6": 4 }
Total #: 24
Throws: { "1": 506, "2": 515, "3": 511, "4": 551, "5": 525, "6": 541 }
Total #: 3149
Throws: { "1": 712, "2": 707, "3": 721, "4": 769, "5": 745, "6": 741 }
Total #: 4395
Throws: { "1": 552, "2": 538, "3": 560, "4": 559, "5": 593, "6": 594 }
Total #: 3396
Throws: { "1": 159, "2": 161, "3": 164, "4": 171, "5": 175, "6": 174 }
Total #: 1004
Throws: { "1": 1081, "2": 1071, "3": 1083, "4": 1039, "5": 1009, "6": 1085 }
Total #: 6368
Throws: { "1": 2, "2": 2, "3": 2, "4": 2, "5": 2, "6": 2 }
Total #: 12
Throws: { "1": 144, "2": 148, "3": 139, "4": 143, "5": 138, "6": 151 }
Total #: 863
Throws: { "1": 307, "2": 330, "3": 333, "4": 307, "5": 332, "6": 326 }
Total #: 1935
Throws: { "1": 218, "2": 219, "3": 215, "4": 225, "5": 207, "6": 204 }
Total #: 1288
Throws: { "1": 706, "2": 673, "3": 674, "4": 725, "5": 689, "6": 676 }
Total #: 4143
Throws: { "1": 805, "2": 756, "3": 809, "4": 828, "5": 793, "6": 751 }
Total #: 4742
Throws: { "1": 150, "2": 143, "3": 143, "4": 157, "5": 153, "6": 152 }
Total #: 898
Throws: { "1": 289, "2": 270, "3": 276, "4": 265, "5": 284, "6": 289 }
Total #: 1673
Throws: { "1": 900, "2": 902, "3": 872, "4": 868, "5": 943, "6": 904 }
Total #: 5389
Throws: { "1": 647, "2": 669, "3": 703, "4": 653, "5": 709, "6": 701 }
Total #: 4082
Throws: { "1": 223, "2": 221, "3": 236, "4": 230, "5": 242, "6": 231 }
Total #: 1383
Throws: { "1": 965, "2": 1059, "3": 993, "4": 1011, "5": 966, "6": 1058 }
Total #: 6052
Throws: { "1": 257, "2": 251, "3": 243, "4": 238, "5": 237, "6": 243 }
Total #: 1469
Throws: { "1": 141, "2": 136, "3": 132, "4": 132, "5": 142, "6": 129 }
Total #: 812
Throws: { "1": 134, "2": 135, "3": 138, "4": 130, "5": 141, "6": 128 }
Total #: 806
Throws: { "1": 144, "2": 133, "3": 141, "4": 145, "5": 134, "6": 132 }
Total #: 829
Throws: { "1": 1675, "2": 1774, "3": 1769, "4": 1817, "5": 1723, "6": 1817 }
Total #: 10575
Throws: { "1": 92, "2": 88, "3": 84, "4": 86, "5": 89, "6": 88 }
Total #: 527
Throws: { "1": 297, "2": 312, "3": 301, "4": 327, "5": 314, "6": 318 }
Total #: 1869
Throws: { "1": 195, "2": 212, "3": 192, "4": 210, "5": 194, "6": 209 }
Total #: 1212
Throws: { "1": 491, "2": 523, "3": 505, "4": 531, "5": 528, "6": 522 }
Total #: 3100
Throws: { "1": 1067, "2": 990, "3": 1003, "4": 1044, "5": 1035, "6": 1090 }
Total #: 6229
Throws: { "1": 83, "2": 87, "3": 87, "4": 81, "5": 86, "6": 84 }
Total #: 508
Throws: { "1": 376, "2": 353, "3": 364, "4": 390, "5": 390, "6": 356 }
Total #: 2229
Throws: { "1": 907, "2": 893, "3": 925, "4": 970, "5": 959, "6": 889 }
Total #: 5543
Throws: { "1": 161, "2": 166, "3": 172, "4": 157, "5": 156, "6": 172 }
Total #: 984
Throws: { "1": 97, "2": 97, "3": 104, "4": 104, "5": 99, "6": 95 }
Total #: 596
Throws: { "1": 262, "2": 265, "3": 287, "4": 285, "5": 272, "6": 269 }
Total #: 1640
Throws: { "1": 639, "2": 615, "3": 625, "4": 678, "5": 654, "6": 671 }
Total #: 3882
Throws: { "1": 157, "2": 149, "3": 155, "4": 150, "5": 161, "6": 148 }
Total #: 920
Throws: { "1": 635, "2": 647, "3": 664, "4": 672, "5": 619, "6": 672 }
Total #: 3909
Throws: { "1": 864, "2": 823, "3": 861, "4": 861, "5": 830, "6": 899 }
Total #: 5138
Throws: { "1": 287, "2": 287, "3": 293, "4": 294, "5": 307, "6": 287 }
Total #: 1755
Throws: { "1": 146, "2": 140, "3": 138, "4": 139, "5": 143, "6": 133 }
Total #: 839
Throws: { "1": 50, "2": 49, "3": 47, "4": 47, "5": 49, "6": 51 }
Total #: 293
Throws: { "1": 185, "2": 173, "3": 175, "4": 186, "5": 177, "6": 190 }
Total #: 1086
Throws: { "1": 668, "2": 656, "3": 610, "4": 632, "5": 650, "6": 634 }
Total #: 3850
Throws: { "1": 451, "2": 428, "3": 424, "4": 459, "5": 419, "6": 442 }
Total #: 2623
Throws: { "1": 1209, "2": 1274, "3": 1312, "4": 1291, "5": 1203, "6": 1304 }
Total #: 7593
Throws: { "1": 174, "2": 170, "3": 168, "4": 169, "5": 158, "6": 158 }
Total #: 997
Throws: { "1": 384, "2": 423, "3": 407, "4": 395, "5": 423, "6": 386 }
Total #: 2418
Throws: { "1": 381, "2": 359, "3": 387, "4": 361, "5": 396, "6": 380 }
Total #: 2264
Throws: { "1": 140, "2": 135, "3": 147, "4": 135, "5": 149, "6": 146 }
Total #: 852
Throws: { "1": 131, "2": 129, "3": 131, "4": 140, "5": 132, "6": 137 }
Total #: 800
Throws: { "1": 622, "2": 662, "3": 638, "4": 667, "5": 678, "6": 660 }
Total #: 3927
Throws: { "1": 478, "2": 467, "3": 461, "4": 441, "5": 480, "6": 456 }
Total #: 2783
Throws: { "1": 214, "2": 208, "3": 210, "4": 200, "5": 195, "6": 196 }
Total #: 1223
Throws: { "1": 160, "2": 172, "3": 169, "4": 156, "5": 157, "6": 169 }
Total #: 983
Throws: { "1": 98, "2": 92, "3": 93, "4": 99, "5": 99, "6": 100 }
Total #: 581
Throws: { "1": 758, "2": 721, "3": 701, "4": 704, "5": 694, "6": 754 }
Total #: 4332
Throws: { "1": 1025, "2": 1016, "3": 1021, "4": 1079, "5": 1021, "6": 1004 }
Total #: 6166
Throws: { "1": 124, "2": 118, "3": 121, "4": 127, "5": 118, "6": 118 }
Total #: 726
Throws: { "1": 478, "2": 497, "3": 481, "4": 490, "5": 456, "6": 452 }
Total #: 2854
Throws: { "1": 1198, "2": 1200, "3": 1155, "4": 1116, "5": 1135, "6": 1231 }
Total #: 7035
Throws: { "1": 139, "2": 152, "3": 142, "4": 142, "5": 144, "6": 150 }
Total #: 869
Throws: { "1": 802, "2": 761, "3": 752, "4": 807, "5": 782, "6": 828 }
Total #: 4732
Throws: { "1": 70, "2": 74, "3": 73, "4": 77, "5": 73, "6": 73 }
Total #: 440
Throws: { "1": 1268, "2": 1192, "3": 1189, "4": 1291, "5": 1264, "6": 1304 }
Total #: 7508
Throws: { "1": 616, "2": 574, "3": 589, "4": 582, "5": 569, "6": 590 }
Total #: 3520
Throws: { "1": 148, "2": 140, "3": 148, "4": 152, "5": 144, "6": 151 }
Total #: 883
Throws: { "1": 218, "2": 214, "3": 228, "4": 236, "5": 236, "6": 219 }
Total #: 1351
Throws: { "1": 191, "2": 205, "3": 191, "4": 206, "5": 189, "6": 208 }
Total #: 1190
---
6-sided die
Number of simulations: 100
Average number of throws to be within 5%: 2590.5
Max: 12732 | Min: 12


Bonus fact: The probability of hitting the minimum of 20 for the 20-sided die is:

20/20 * 19/20 * 18/20 * ... * 1/20 = 0.00000232019615953125 %

dice-fairness's People

Contributors

brunnerh avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.