When the program can step into the loop the coinFlip that we created is called and the result is saved to the element in our array.
We use n so that we do not exceed our array size. The i in the header will be the index that controls whether or not the loop is done again.
#Coin flip random code
For Loop #perform desired numbered of flips at required probability set above for i in range(0, n): fullResults = coinFlip(probability) i+=1įor loops are basic code structures, but just in case lets walk through what is happening. Now the for loop can be created that will call coinFlip. This will return only a 0 or 1, false or true. def coinFlip(p): #perform the binomial distribution (returns 0 or 1) result = np.random.binomial(1,p) #return flip to be added to numpy array return resultĪs you can see, the number of trials will be set to 1. The program needs to define it before it can be used. Be sure to place this before the main area.
It will return the result from the binomial. We will call it coinFlip and it will need to take in the probability for the events. With that decision, let’s create the method. This will allow for easy access if we decide to build upon the program in the future. Since we are about code re-usability we will create a function that houses the binomial distribution. One that requires us to create a function that does the ‘flip’ or does the ‘flip’ in the for loop. This is the point where we can go down two paths.
#initiate array fullResults = np.arange(n) Using np.arange will create an array with 10 elements filled with 0 to 9 value in ascending order. We also know that we will need to initiate an array to store the flips. Marking any variables that can be safely changed without any negative effects is a great practice to get into as well. In my code, I like to mark where the main area is for easy searching just in case there are several methods created in that program. '''Main Area''' #probability of heads vs. We know that we will need those since we will be using np.random.binomial and that requires the number of trials and the probability. Next, we’ll create the n and p variables. Let’s come back down to the ground and get coding.įirst things first, we need to import numpy. Thank goodness numpy can do all of that! Code So we will need an array to store the results in. This opens the question, do we want to see each flip and track it or are we satisfied with just the number of times a positive (1) event occurred? I’m on board with seeing an array of events, just for fun. We know now that for the binomial distribution to work we need two variables, n and p. P is the probability, n is the number of trials ran, and N is the number of successes. The call returns a 0 or 1 to represent one of the two events. Binomial distribution, as its name suggests, can perform a ‘coin flip’ of two events happening. This is a method in the random class and it takes in the number of trials (n) and the probability of the event occurring (p). Specifically numpy’s binomial distribution, np.random.binomial(n,p). Since we are using python, a mathematically focused language, we will use probability. How are we going to simulate a coin flip? We can’t give the computer a bitcoin and tell it to flip it. If the description mentioned biased or weighted coin then the probability would be adjusted. Since ‘fair’ is used in the project description we know that the probability will be a 50% chance of getting either side. A coin is made up of two halves, head and tails. We know that we will be doing a fair coin flip. Now that we have a bird’s eye view of our project lets dissect it. Your standard is only as high as when no one is looking. Yes, even with a simple project like this, habits are made in and out of the public eye. Programmers have an amazing amount of creative freedom (well depending on your (micro)manager, I empathize with you if you are in that boat ❤), you can start to lose focus on what the main focus is on a project. Create A Blueprintīefore we start tapping away, creating a program that makes a computer do a mundane task such as flipping a coin, let’s fly up 10,000 feet and get our bearings. Let’s get into what you clicked for, coding a fair coin flip in python. Being able to critique your code is a critical programmer skill, but that is another post. You may even get an insight into your coding habits. It seems like a simple project, but it can be done in many different ways. Coding a fair coin flip is kind of a right of passage when it comes to python. To master anything, you need to start at the beginning, understanding the basics.