"Algorithm" simulates throwing dice

Source: Internet
Author: User

Simulates the roll of a dice. The following code calculates the exact probability distribution of the sum of each of the two dice:

int SIDES = 6;double[] dist = new Double[2*sides+1];for (int i = 1; I <= SIDES; i++) for     (int j = 1; I <= SIDES; J + +)        Dist[i+j] + = 1.0;for (int k = 2; k <= 2*sides; k++)    dist[k]/= 36.0;

The value of dist[i] is the probability that the sum of two dice is I. Simulate n-roll dice with an experiment and calculate two 1 to

The sum of random integers between 6 records the frequency at which each value appears to verify their probability. n How big will it be to guarantee you

The empirical data and accurate data to match the degree of three digits after the decimal point?

Experiment Code:

1  Packagecom.beyond.algs4.experiment;2 3 ImportJava.math.BigDecimal;4 5 ImportCom.beyond.algs4.lib.StdRandom;6 7 8  Public classSides {9 Ten     Private Static intSIDES = 6; One      A     Private Double[] dist =New Double[2*sides + 1]; -      -      Public Double[] Getdist () { the         returnDist; -     } -  -      Public voidSetdist (Double[] Dist) { +          This. Dist =Dist; -     } +  A      Public voidprobability () { at          for(inti = 1; I <= SIDES; i++) { -             for(intj = 1; J <= SIDES; J + +) { -Dist[i + j] + = 1.0; -            }  -         } -          for(intK = 2; K <= 2*sides; k++) { inDist[k]/= 36.0; -         } to     } +      -      Public voidprint () { the          for(inti = 0; i < dist.length; i++) { * System.out.println ( $String.Format ("Probability of [%d] is:%f", I, dist[i]));Panax Notoginseng         } -     } the      +      Public Static classEmulator { A         Private intN = 100; the          +         Private Double[] dist =New Double[2*sides + 1]; -          $          Public intGetn () { $             returnN; -         } -  the          Public voidSETN (intN) { -N =N;Wuyi         } the  -          Public Double[] Getdist () { Wu             returnDist; -         } About  $          Public voidSetdist (Double[] Dist) { -              This. Dist =Dist; -         } -  A          Public voidEmulator () { +              for(inti = 0; i < N; i++) { the                 intA = Stdrandom.uniform (1, 7); -                 intb = Stdrandom.uniform (1, 7); $Dist[a + b] + = 1.0; the             } the              for(intK = 2; K <= 2*sides; k++) { theDIST[K]/=N; the             } -         }         in          the          Public intN (Sides Sides) { the              for(inti = 1; I <= 100; i++) { About                  This. SETN (NewDouble (Math.pow (ten, I)). Intvalue () * This. N); the                  This. Emulator (); the                 BooleanAPPR =true; the                  for(intK = 2; K <= 2*sides; k++) { +                     Doubles = This. Getdist () [K]; -BigDecimal bs =NewBigDecimal (s); the                     DoubleS1 = Bs.setscale (2, Bigdecimal.round_down). Doublevalue ();Bayi                     Doublet =sides.getdist () [K]; theBigDecimal BT =NewBigDecimal (t); the                     DoubleT1 = Bt.setscale (2, Bigdecimal.round_down). Doublevalue (); -                     if(S1! =T1) { -APPR =false; the                          Break; the                     } the                 } the                 if(appr) { -                     return  This. GETN ();  the                 } the             } the             return0;94         } the          the          Public voidprint () { the              for(inti = 0; i < dist.length; i++) {98 System.out.println ( AboutString.Format ("Probability of [%d] is:%f", I, dist[i])); -             }101         }102         103         104     } the     106      Public Static voidMain (string[] args) {107Sides Sides =NewSides ();108 sides.probability ();109  theEmulator e =NewEmulator ();111         intN =E.N (sides); theSystem.out.println (String.Format ("The N is:%d", N));113System.out.println ("Actual:"); the sides.print (); theSYSTEM.OUT.PRINTLN ("Experiment:"); the e.print ();117     }118 119}


Experimental results:

1The N is:1000000002 Actual:3probability of [0] is:0.0000004probability of [1] is:0.0000005probability of [2] is:0.0277786probability of [3] is:0.0555567probability of [4] is:0.0833338probability of [5] is:0.1111119probability of [6] is:0.138889Tenprobability of [7] is:0.166667 Oneprobability of [8] is:0.138889 Aprobability of [9] is:0.111111 -Probability of [ten] is:0.083333 -Probability of [one] is:0.055556 theProbability of [] is:0.027778 - Experiment: -probability of [0] is:0.000000 -probability of [1] is:0.000000 +probability of [2] is:0.027754 -probability of [3] is:0.055544 +probability of [4] is:0.083374 Aprobability of [5] is:0.111130 atprobability of [6] is:0.138897 -probability of [7] is:0.166751 -probability of [8] is:0.138832 -probability of [9] is:0.111088 -Probability of [ten] is:0.083306 -Probability of [one] is:0.055517 inProbability of [] is:0.027807

Results Analysis:
Run multiple times, the n value is typically 100000000, and there are other values that are unstable.

Additional notes:

1. With n=100 as the initial value, the loop executes the probability of simulating n times, and if the anastomosis is not satisfied then n is incremented by 10 times times until the appropriate n value is found. (need to be improved with while (find))

2. The realization of "three digits after the decimal point" adopts the method of three-bit round_down after decimal point, which needs to be improved

3. The n value is not a stable result of 100000000 for each run, it can be executed randomly multiple times to analyze the distribution of n values.

Resources:

Algorithm fourth edition She Luyun algorithms Fourth Edition [US] Robert Sedgewick, Kevin Wayne

http://algs4.cs.princeton.edu/home/

SOURCE Download Link:

Http://pan.baidu.com/s/1c0jO8DU

"Algorithm" simulates throwing dice

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.