<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Gauri Shanker</title>
    <description>The latest articles on Forem by Gauri Shanker (@gsbansal10).</description>
    <link>https://forem.com/gsbansal10</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F196318%2F99c9bdb0-e9ef-48f8-8e7e-c04184b6b61d.jpeg</url>
      <title>Forem: Gauri Shanker</title>
      <link>https://forem.com/gsbansal10</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/gsbansal10"/>
    <language>en</language>
    <item>
      <title>One Dimensional Geometric probability and python</title>
      <dc:creator>Gauri Shanker</dc:creator>
      <pubDate>Thu, 03 Sep 2020 08:26:57 +0000</pubDate>
      <link>https://forem.com/gsbansal10/one-dimensional-geometric-probability-and-python-32ji</link>
      <guid>https://forem.com/gsbansal10/one-dimensional-geometric-probability-and-python-32ji</guid>
      <description>&lt;p&gt;Recently I published a video on YouTube solving a problem in One Dimensional Probability. The problem goes as follows - &lt;strong&gt;What is the probability that a random number chosen between 0 and 3 will be closer to 0 than it is to 1?&lt;/strong&gt; You can watch the video here - &lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Ld0FHvadecM"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;How do you approach this problem? You cannot use the traditional formula of probability - &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Probability = (Number of desired events / Number of all possible events)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You might be thinking why can't we use this formula? &lt;/p&gt;

&lt;p&gt;Well, the reason for that is that the above formula can be used only when we have a countable number of events. But in this case, the number of desired as well as all possible events is infinite. Consider this number line - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NU5w2ZCL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p6xj5dx0owfqtzdcbag0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NU5w2ZCL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/i/p6xj5dx0owfqtzdcbag0.jpg" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There can be infinite numbers on this line segment of 3 units so our above formula for calculating probabilities fails here. The only recourse left before us is to use Geometric Probability.&lt;/p&gt;

&lt;p&gt;When the events cannot be counted, we can represent them as geometric figures. If we have 1 independent variable, we use lengths to represent them, if we have 2, we use areas and if we have 3, we use volumes.&lt;/p&gt;

&lt;p&gt;Since here the number of independent variables is just 1, we can represent it using lengths. &lt;/p&gt;

&lt;p&gt;Now that we have laid the groundwork, its time to tackle the question. Consider that, on the above-shown number line, any number will be equidistant from 0 and 1 when it is equal to 0.5 since its exactly mid-way between the two. &lt;/p&gt;

&lt;p&gt;If any number is to the left of 0.5, it will closer to 0 and if it is to the right of 0.5 all the way up to 3, it will be closer to 1. Thus we can say that the segment from 0 to 0.5 units represents our desired outcome and the segment from 0 to 3 represents all possible outcomes. The formula for probability now reduces to - &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Probability = (length of the desired line segment/length of the entire possible line segment)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Putting in the values, we get 

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;0.6/3=1/6=0.1666....
 0.6/3 = 1/6 = 0.1666....
&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;6&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord"&gt;3&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mord"&gt;6&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;span class="mrel"&gt;=&lt;/span&gt;&lt;span class="mspace"&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mord"&gt;0&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;1&lt;/span&gt;&lt;span class="mord"&gt;6&lt;/span&gt;&lt;span class="mord"&gt;6&lt;/span&gt;&lt;span class="mord"&gt;6&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;span class="mord"&gt;.&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/p&gt;

&lt;p&gt;Well, that's the theory - right! Its time to test it using python. &lt;/p&gt;

&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;

&lt;p&gt;In the first step, import the &lt;code&gt;random&lt;/code&gt; function from the &lt;code&gt;random&lt;/code&gt; module because we will need to generate random numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;from&lt;/span&gt; &lt;span class="nn"&gt;random&lt;/span&gt; &lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Second, make a function that will generate a random number between 0 and 3 and perform the proximity check with zero or 1.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;one_trial&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="c1"&gt;# Code goes here
&lt;/span&gt;    &lt;span class="c1"&gt;# and here
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We name this function one_trial. Inside this function, we generate a random number x using the random function that we imported earlier. Remember that this function produces values between 0 and 1 so we will need to multiply it with 3 so that it produces random numbers between 0 and 3.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;one_trial&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next, test whether this number x is closer to 0 or 1 by checking if it is less than 0.5. If yes, then return 1, if no, then return 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;one_trial&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's all there is to this function.&lt;/p&gt;

&lt;p&gt;Now we just have to run this function numerous times and check whether the result comes the same as we determined using mathematics. &lt;/p&gt;

&lt;p&gt;Let us check for ten thousand iterations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next, we make a variable 'success' which will track how many times have we gotten the desired outcome that is the number less than 0.5. We initialize this variable with 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;success&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Next, we create a &lt;code&gt;for&lt;/code&gt; loop which will perform this experiment N times and in every iteration, the result from each trial function will be added to the success variable i.e. 1 if we got success and 0 if we got no success.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;success&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;one_trial&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;After the &lt;code&gt;for&lt;/code&gt; loop ends, its time to print the results in the form of probability i.e. success divided by iterations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f"The probability is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;success&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Its time to run this function. Remember that the expected answer is 0.1666... &lt;/p&gt;

&lt;p&gt;I am getting 0.1761 for 10000 iterations and 0.16623 for a million iterations. &lt;/p&gt;

&lt;p&gt;Thus our calculations of geometric probability are indeed correct guys. I hope you enjoyed this article. &lt;/p&gt;

&lt;p&gt;Thanks and bye-bye. I will see you in the next one. &lt;/p&gt;

</description>
      <category>python</category>
      <category>probability</category>
      <category>maths</category>
      <category>dice</category>
    </item>
    <item>
      <title>Simulating the flip of a coin using python</title>
      <dc:creator>Gauri Shanker</dc:creator>
      <pubDate>Wed, 02 Sep 2020 07:49:42 +0000</pubDate>
      <link>https://forem.com/gsbansal10/simulating-the-flip-of-a-coin-using-python-1471</link>
      <guid>https://forem.com/gsbansal10/simulating-the-flip-of-a-coin-using-python-1471</guid>
      <description>&lt;p&gt;Recently I published a YouTube video in which I calculated the probability of a head on a coin flip. The catch here is that it is also to be demonstrated using a computer program simulating a coin flip for at least a million iterations. You can watch the video here - &lt;br&gt;
&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/Aexx33WA6ig"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Mathematically speaking, the probability of an event is calculated by the following formula -&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Probability of an event = 

&lt;span class="katex-element"&gt;
  &lt;span class="katex"&gt;&lt;span class="katex-mathml"&gt;(Number of favorable events)/(Number of all possible events)(Number~of~favorable~events)/(Number~of~all~possible~events)&lt;/span&gt;&lt;span class="katex-html"&gt;&lt;span class="base"&gt;&lt;span class="strut"&gt;&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathdefault"&gt;N&lt;/span&gt;&lt;span class="mord mathdefault"&gt;u&lt;/span&gt;&lt;span class="mord mathdefault"&gt;m&lt;/span&gt;&lt;span class="mord mathdefault"&gt;b&lt;/span&gt;&lt;span class="mord mathdefault"&gt;e&lt;/span&gt;&lt;span class="mord mathdefault"&gt;r&lt;/span&gt;&lt;span class="mspace nobreak"&gt; &lt;/span&gt;&lt;span class="mord mathdefault"&gt;o&lt;/span&gt;&lt;span class="mord mathdefault"&gt;f&lt;/span&gt;&lt;span class="mspace nobreak"&gt; &lt;/span&gt;&lt;span class="mord mathdefault"&gt;f&lt;/span&gt;&lt;span class="mord mathdefault"&gt;a&lt;/span&gt;&lt;span class="mord mathdefault"&gt;v&lt;/span&gt;&lt;span class="mord mathdefault"&gt;o&lt;/span&gt;&lt;span class="mord mathdefault"&gt;r&lt;/span&gt;&lt;span class="mord mathdefault"&gt;a&lt;/span&gt;&lt;span class="mord mathdefault"&gt;b&lt;/span&gt;&lt;span class="mord mathdefault"&gt;l&lt;/span&gt;&lt;span class="mord mathdefault"&gt;e&lt;/span&gt;&lt;span class="mspace nobreak"&gt; &lt;/span&gt;&lt;span class="mord mathdefault"&gt;e&lt;/span&gt;&lt;span class="mord mathdefault"&gt;v&lt;/span&gt;&lt;span class="mord mathdefault"&gt;e&lt;/span&gt;&lt;span class="mord mathdefault"&gt;n&lt;/span&gt;&lt;span class="mord mathdefault"&gt;t&lt;/span&gt;&lt;span class="mord mathdefault"&gt;s&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;span class="mord"&gt;/&lt;/span&gt;&lt;span class="mopen"&gt;(&lt;/span&gt;&lt;span class="mord mathdefault"&gt;N&lt;/span&gt;&lt;span class="mord mathdefault"&gt;u&lt;/span&gt;&lt;span class="mord mathdefault"&gt;m&lt;/span&gt;&lt;span class="mord mathdefault"&gt;b&lt;/span&gt;&lt;span class="mord mathdefault"&gt;e&lt;/span&gt;&lt;span class="mord mathdefault"&gt;r&lt;/span&gt;&lt;span class="mspace nobreak"&gt; &lt;/span&gt;&lt;span class="mord mathdefault"&gt;o&lt;/span&gt;&lt;span class="mord mathdefault"&gt;f&lt;/span&gt;&lt;span class="mspace nobreak"&gt; &lt;/span&gt;&lt;span class="mord mathdefault"&gt;a&lt;/span&gt;&lt;span class="mord mathdefault"&gt;l&lt;/span&gt;&lt;span class="mord mathdefault"&gt;l&lt;/span&gt;&lt;span class="mspace nobreak"&gt; &lt;/span&gt;&lt;span class="mord mathdefault"&gt;p&lt;/span&gt;&lt;span class="mord mathdefault"&gt;o&lt;/span&gt;&lt;span class="mord mathdefault"&gt;s&lt;/span&gt;&lt;span class="mord mathdefault"&gt;s&lt;/span&gt;&lt;span class="mord mathdefault"&gt;i&lt;/span&gt;&lt;span class="mord mathdefault"&gt;b&lt;/span&gt;&lt;span class="mord mathdefault"&gt;l&lt;/span&gt;&lt;span class="mord mathdefault"&gt;e&lt;/span&gt;&lt;span class="mspace nobreak"&gt; &lt;/span&gt;&lt;span class="mord mathdefault"&gt;e&lt;/span&gt;&lt;span class="mord mathdefault"&gt;v&lt;/span&gt;&lt;span class="mord mathdefault"&gt;e&lt;/span&gt;&lt;span class="mord mathdefault"&gt;n&lt;/span&gt;&lt;span class="mord mathdefault"&gt;t&lt;/span&gt;&lt;span class="mord mathdefault"&gt;s&lt;/span&gt;&lt;span class="mclose"&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/span&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here, in this scenario, the favorable event is the appearance of 'Head' and all possible events are any of the faces 'Heads or Tails'. Thus the number of favorable events is 1 whereas the number of all possible events is 2. Thus according to the formula of probability- we get a probability of 1/2 or 50% approx.&lt;/p&gt;

&lt;p&gt;Well, now is the time to simulate that using python. Let's get moving -&lt;/p&gt;

&lt;p&gt;First of all, import the &lt;code&gt;random&lt;/code&gt; module because we have to randomly select a face of the coin.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;random&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now, its time to create a function, we name it &lt;code&gt;experiment&lt;/code&gt;. This function will simulate one coin flip and return 1 if we get a Head and 0 if we got a Tail.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;experiment&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt;
    &lt;span class="n"&gt;faces&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;'T'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;'H'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# all possible faces
&lt;/span&gt;    &lt;span class="n"&gt;top_face&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;faces&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="c1"&gt;# randomly choose a face
&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;top_face&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s"&gt;'H'&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;# Checking if we got a head
&lt;/span&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="c1"&gt;# return 1 if success
&lt;/span&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="c1"&gt;# otherwise return 0
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now that we have created our function, its time to test it for a million iterations -&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;headCounter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="c1"&gt;# variable to count the number of times we get heads
# conduct the experiment a million times and count the heads
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1000000&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="n"&gt;headCounter&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;experiment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;# Print the results as percentage of total number of iterations
&lt;/span&gt;&lt;span class="k"&gt;print&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;f"The probability of getting head is &lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="n"&gt;headCounter&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;%"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In my tests, I am consistently getting numbers close to 50 such as 50.0021% and 50.0017% which is in-line with our calculations.&lt;/p&gt;

&lt;p&gt;Hope you enjoyed this article. You can also watch my YouTube video here on which this article is based.&lt;/p&gt;

&lt;p&gt;Thanks and bye guys. I will see you in the next one.&lt;/p&gt;

</description>
      <category>probability</category>
      <category>python</category>
      <category>simulation</category>
      <category>coinflip</category>
    </item>
  </channel>
</rss>
