Java - Monte Carlo Coin Simulation Tutorial #3
Today we will be creating a simple program in Java that simulates the Monte Carlo coin scenario. Who's ready to find out if they can win at coins?
What is Monte Carlo Simulation?:
Monte Carlo simulation is a process of random numerical experiments that are used in analysis for computation. This type of simulation is perfect for us because we are using the random number generator in java, for the coin flip, and in real life, no one really knows the accurate outcome of a coin flip!
What this means depends on the application, but typically they should pass a series of statistical tests. Testing that the numbers are uniformly distributed or follow another desired distribution when a large enough number of elements of the sequence are considered is one of the simplest, and most common ones. Weak correlations between successive samples is also often desirable/necessary.
There is more information on this on wikipedia right here!
Below, I have coded a small java application that simulates many rounds of coin tossing. If you know java, then this will be easy to understand. If you are a gambler, you may want to stop before you lose more money than you make at any casino. As you see, it's almost realistic, but it could be fined tuned better if we incorporate a precise probability method that takes into account previous trials to better predict the outcome (I guess). I could also code a Monte Carlo simulation for a slot machine if anyone is interested.
Check out the source code, it's easy to follow.
When you run the application in NetBeans IDE, you get the following results:
What is it doing? Basically it's simulating coin tosses between the player and the dealer. If the player guesses correctly they win $1 and if they lose, they lose $1. The code uses the random number generator and it continues going for the user input specified matches and starting amount of cash. There is no object-oriented programming techniques used for this application. It's pretty straightforward.
That's it for this tutorial! The next one we will start creating classes to make objects for dices! If you have a question or a thought please leave a comment.
Comments powered by CComment