Dice, dice, lets play nice! Ready to roll for that winning number? You might as well test it out yourself or create you own dice simulation in java!

What is Monte Carlo Simulation?:

Basically a simple simulation based off an analysis of random number in occuring trials or events. More info in the previous tutorial here: Java - Monte Carlo Coin Simulation Tutorial #3.

About The Program:

If you ever wondered how random dice games worked now is your lucky day. Below I created a small Java application that can mimic dice rolling simulation. It will also show you the results and you may modify the code to your own fitting. Be sure to give me credit if you do use it please. Giving credit is a good practice, especially if it's more than ten lines of code. In this program, the user can create objects of class dice, and specify their names. I was going to add the numbers of side, but you can edit the variables so that you can roll die of different numbers! Below will be the code and a quick explanation followed by some screenshots.

Source Code:

Check out the source code, it's easy to follow.

{spoiler spoilerID,Show Code, Hide Code}{/spoiler}

Code Output:

When you run the application in NetBeans IDE, you get the following results. Here I ran two simulations to show how the performance is with many trials in the program.

Simulation One:

Simulation Two:     

Code Explanation:

For the first simulation I first ran the simulation for 100 rolls and the five consecutive rolls for seven for two dice. As you can see in this simulation we were unable to get a five streak roll of 7. However, out of the 100 rolls, we got 16 sevens and the average to get a 7 was 5.25 rolls.

For the second simulation, we increase the number of rolls! As you can see in this simulation we were able to get a five streak roll of 7, 13 times. However, out of the 100,000 rolls, we got 16,568 sevens and the average to get a 7 was about 5 rolls.

Thank You:

That's it for this tutorial! I may or may not continue creating java tutorials, because I'm actually more of a C# kind of guy despite there being not much tutorial on the language here, haha! If you have a question or a thought please leave a comment.