<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://zirkelc.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 19 Jun 2026 08:14:56 GMT</lastBuildDate><atom:link href="https://zirkelc.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Calculating π with the Monte Carlo Simulation]]></title><description><![CDATA[When I was in university, I first learned about the Monte Carlo Simulation as way to calculate \(\pi\) (pi). The simple – yet genius – idea behind this concept just blew my mind. Calculating an infinite number like pi doesn't sound like fun for most ...]]></description><link>https://zirkelc.hashnode.dev/calculating-pi-with-the-monte-carlo-simulation</link><guid isPermaLink="true">https://zirkelc.hashnode.dev/calculating-pi-with-the-monte-carlo-simulation</guid><category><![CDATA[React]]></category><category><![CDATA[TypeScript]]></category><category><![CDATA[Mathematics]]></category><dc:creator><![CDATA[Chris Zirkel]]></dc:creator><pubDate>Thu, 07 Oct 2021 15:27:16 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1633620148930/xSaCOLwnY.jpeg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When I was in university, I first learned about the Monte Carlo Simulation as way to calculate \(\pi\) (pi). The simple – yet genius – idea behind this concept just blew my mind. Calculating an infinite number like pi doesn't sound like fun for most people. Still, seeing the number getting more and more precise continues to amaze me. In order to refresh some old memories, I decided to implement an interactive simulation in React and TypeScript.</p>
<h2 id="let-it-rain">Let It Rain</h2>
<p>Let me explain the idea behind the Monte Carlo Simulation with an analogy to rain. Take a sheet of paper and draw a unit square (whose sides have length 1) on it. Inside this unit square, draw a quarter circle with a radius of 1. It will look like this:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1633618362664/3J1oJmkk6.png" alt="image.png" /></p>
<p>Now, let's imagine it is raining on this unit square with perfect randomness. The raindrops are going to be evenly distributed on the unit square. Some raindrops will lie inside the quarter circle (i.e. blue dots), and some will lie outside of it (i.e. red dots). Logically, a lot more raindrops will fall inside the quarter circle than outside of it. This is what it looks with 1000 raindrops:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1633618443242/y_ASD1tcU.png" alt="image.png" /></p>
<p>Interestingly, the fraction of raindrops inside the quarter circle over the total number of raindrops will constantly change as we generate more raindrops. This is due to the <a target="_blank" href="https://en.wikipedia.org/wiki/Law_of_large_numbers">law of large numbers</a> and the fact that we reach ever better distribution. Let's keep this fraction in mind as we're going to need it in the next step.   </p>
<h2 id="give-me-the-math">Give Me The Math</h2>
<p>I'm going to briefly explain the underlying math principle. There are plenty of good articles on the Internet for more detailed information. 
The method is based on the mathematical formula for calculating the area of a unit circle (i.e. the one with radius 1):
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1633511174949/c9sCKZ_UU.png" alt="image.png" /></p>
<p>We are going to cut the unit circle in four equal segments. The derived quarter circle (i.e. the blue area) still has a radius of 1 and its area is defined by the following formula:
<img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1633511245413/qk7kbTQK4.png" alt="image.png" />
Now interestingly, the quarter circle fits perfectly well into a unit square (i.e. the red area) with an edge length of 1. We know that the unit square has an area of \(1\times1\) and the quarter circle partly overlaps with this area as defined by the previous formula for \(A_{quarter}\). We must assume the fraction to which it overlaps – and the way of doing that – is by generating random points within the unit square (e.g. we let it rain). </p>
<p>This fraction can be defined as \( \frac{drops\;inside\;the\;quarter\;circle}{total\;number\;of\;drops\;generated}\) or abbreviated as \(\frac{i}{t}\). From here, we can build an equation with a fraction of the quarter circle area over unit square are equal the fraction of the drops inside the quarter circle over the total number of drops. This equation must then be solved for \(\pi\) and leads us to the following equation:</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1633511391851/TngEnhvJw.png" alt="image.png" /></p>
<h2 id="interactive-simulation">Interactive Simulation</h2>
<p>I have implemented an interactive simulation in React and Typescript. The <a target="_blank" href="https://zirkelc.github.io/monte-carlo-pi/">app</a> uses <a target="_blank" href="https://plotly.com/javascript/">Plotly.js</a> to draw the unit square, the quarter circle and the raindrops. I have added a few buttons to randomly generate 1, 10, 100 or 1000 of raindrops. The raindrops are colored blue if they fall inside the quarter circle, otherwise they are colored red. There's also a special button labelled as <strong>Let It Rain</strong> to continuously generate raindrops as if it were raining. After each update, the approximate value of Pi is calculated again based on the newly generated raindrops. The more raindrops, the more accurate the value of Pi.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1633523217952/BrDkScslr.png" alt="image.png" /></p>
<h2 id="links">Links</h2>
<ul>
<li><a target="_blank" href="https://zirkelc.github.io/monte-carlo-pi/">Interactive Simulation App</a> </li>
<li><a target="_blank" href="https://github.com/zirkelc/monte-carlo-pi">Source Code on GitHub</a> </li>
<li><a target="_blank" href="https://de.wikipedia.org/wiki/Monte-Carlo-Simulation">Monte Carlo Simulation</a></li>
</ul>
]]></content:encoded></item><item><title><![CDATA[Debug a React App in VSCode]]></title><description><![CDATA[When developing a React app (or any JavaScript app), I heavily use console.log() for debugging purposes if something is not running as expected. Only if it's really tricky, I use the VSCode debugger. It's not that I don't like debuggers, debugging JS...]]></description><link>https://zirkelc.hashnode.dev/debug-a-react-app-in-vscode</link><guid isPermaLink="true">https://zirkelc.hashnode.dev/debug-a-react-app-in-vscode</guid><category><![CDATA[React]]></category><category><![CDATA[Visual Studio Code]]></category><category><![CDATA[Google Chrome]]></category><category><![CDATA[debugging]]></category><dc:creator><![CDATA[Chris Zirkel]]></dc:creator><pubDate>Thu, 26 Aug 2021 11:13:46 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1629976754714/8VB55SCvq.png" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>When developing a React app (or any JavaScript app), I heavily use <code>console.log()</code> for debugging purposes if something is not running as expected. Only if it's really tricky, I use the VSCode debugger. It's not that I don't like debuggers, debugging JS is just not as convenient as it is in other programming languages. </p>
<h2 id="problem">Problem</h2>
<p>Usually, the React app is started with <code>npm run start/yarn start</code> (react-scripts start) and it runs on <strong>localhost:3000</strong> and hot reloads when making file changes. A new Chrome tab is opened by React and I just keep this tab open forever. If I need to check the value of a certain variable, I log it to the console and check the output on Chrome Dev Tools.</p>
<p>On the other hand, VSCode offers two debugging options for JS apps: Launching the debugger with a new browser window or attaching the debugger to the already running app on a certain browser window/tab. Until now, I was only using the first option of launching a new window because I wasn't able to get the second option working. Unfortunately, launching a new browser means you have to navigate to the page you actually want to debug and you lose all your state (e.g. form inputs). So effectively you end up with two instances of the same app. And that's the reason I didn't use the debugger in the first place.</p>
<p><img src="https://cdn.hashnode.com/res/hashnode/image/upload/v1629969950558/Ov69WubDM.png" alt="2021-08-26_11-25-16.png" /></p>
<p>However, I didn't want to accept that issue any longer and decided to figure out what I'm missing. Here are my findings!</p>
<h2 id="start-react-app-with-remote-debugging">Start React App with Remote Debugging</h2>
<p>In order to debug JS apps, the browser has to be started with remote debugging enabled. For example, Chrome has to be started with the flag <code>--remote-debugging-port=9222</code>. When you click debug on VSCode it does exactly that: It starts a new browser window with this command line argument. Unfortunately, the default React start script launches a browser without remote debugging (maybe for safety reasons?). 
However, the <a target="_blank" href="https://create-react-app.dev/docs/advanced-configuration">advanced configuration</a> allows us to change the browser and how it is launched by setting two environment variables <code>BROWSER</code> and <code>BROWSER_ARGS</code>:</p>
<pre><code class="lang-json"><span class="hljs-string">"scripts"</span>: {
    <span class="hljs-attr">"start"</span>: <span class="hljs-string">"BROWSER='google chrome' BROWSER_ARGS='--remote-debugging-port=9222' react-scripts start"</span>,
    ...
}
</code></pre>
<p>The <a target="_blank" href="https://github.com/sindresorhus/open#app">name of the browser</a> depends on the operating system. For example, Chrome is <code>google chrome</code> on macOS, <code>google-chrome</code> on Linux and <code>chrome</code> on Windows. </p>
<p>You have to close Chrome completely before running this script. If Chrome is already running on your system, this React start script will not create a new window but will create a new tab on your existing Chrome window. My assumption is that you enable remote debugging on process level and if you already started Chrome by clicking on the icon, remote debugging is not enabled by default. Then, when React wants to launch a new window, Chrome internally checks if it can use an existing window or if it has to create a new window. In my case, it re-used the existing Chrome window and didn't enable remote debugging. I guess that's also the reason I was not able to attach the VSCode debugger to my running React app.</p>
<h2 id="start-chrome-always-with-remote-debugging">Start Chrome Always with Remote Debugging</h2>
<p>Another more convenient option is to start Chrome always with remote debugging enabled. On <a target="_blank" href="https://stackoverflow.com/a/56457835/1967693">Windows</a>  it's straightforward as you just have to right-click on the Chrome shortcut, go to properties and append the above command line argument to the target field like this: <code>C:\Program Files (x86)\Google\Chrome\Application\chrome.exe --remote-debugging-port=9222 -- "%1"</code></p>
<p>On macOS it's trickier. I've found two options on the Internet that I will include in the following. In both cases you end up with a new Chrome app that you can add to the dock to replace the original app. It's a kind of shortcut that will simply start Chrome with command line arguments.</p>
<h3 id="create-a-custom-chrome-app">Create a Custom Chrome App</h3>
<p>This <a target="_blank" href="https://stackoverflow.com/questions/51563287/how-to-make-chrome-always-launch-with-remote-debugging-port-flag/58457229#58457229">answer on StackOverflow</a> describes how to create a custom Chrome app only with a text editor. It also includes a nice debug icon that you can use to distinguish the custom  app from the original app.</p>
<h3 id="using-automator">Using Automator</h3>
<p>There's another <a target="_blank" href="http://kynatro.com/blog/2018/01/11/chrome-debugging-in-vscode/">solution by Dave Shepard</a> on how to use Automator to create a custom Chrome app. Automator is a macOS standard tool to create apps and workflows composed of multiple actions. In my case, I created an app that runs a shell script to start Chrome with remote debugging enabled.
Please refer to the original blog post for detailed instructions.</p>
<h2 id="debugging-in-vscode">Debugging in VSCode</h2>
<p>Now that we have Chrome up and running with remote debugging enabled, we can head back to VSCode for debugging the React app. VSCode manages its debug configurations in the file <strong>.vscode/launch.json</strong>:</p>
<pre><code class="lang-json">{
    <span class="hljs-comment">// Use IntelliSense to learn about possible attributes.</span>
    <span class="hljs-comment">// Hover to view descriptions of existing attributes.</span>
    <span class="hljs-comment">// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387</span>
    <span class="hljs-attr">"version"</span>: <span class="hljs-string">"0.2.0"</span>,
    <span class="hljs-attr">"configurations"</span>: [
        {
            <span class="hljs-attr">"name"</span>: <span class="hljs-string">"Launch Chrome"</span>,
            <span class="hljs-attr">"request"</span>: <span class="hljs-string">"launch"</span>,
            <span class="hljs-attr">"type"</span>: <span class="hljs-string">"pwa-chrome"</span>,
            <span class="hljs-attr">"url"</span>: <span class="hljs-string">"http://localhost:3000"</span>,
            <span class="hljs-attr">"webRoot"</span>: <span class="hljs-string">"${workspaceFolder}"</span>
        },
        {
            <span class="hljs-attr">"name"</span>: <span class="hljs-string">"Attach to Chrome"</span>,
            <span class="hljs-attr">"port"</span>: <span class="hljs-number">9222</span>,
            <span class="hljs-attr">"request"</span>: <span class="hljs-string">"attach"</span>,
            <span class="hljs-attr">"type"</span>: <span class="hljs-string">"pwa-chrome"</span>,
            <span class="hljs-attr">"urlFilter"</span>: <span class="hljs-string">"http://localhost:3000/*"</span>, <span class="hljs-comment">// use urlFilter instead of url!</span>
            <span class="hljs-attr">"webRoot"</span>: <span class="hljs-string">"${workspaceFolder}"</span>
        }
    ]
}
</code></pre>
<p>There are two debug configurations. The first will create a new Chrome window with remote debugging enabled on the url <strong>http://localhost:3000</strong>, while the second will  attach the debugger to an existing Chrome window. The important things I'd like to mention here are the <code>port</code> and the <code>urlFilter</code> properties. The port number must match the remote debugging port from the command line argument above (it's not the port on which the development server runs, e.g. 3000). The url filter expression will search for a page with this url.</p>
<p>In VSCode we can simply put a breakpoint in our component which is currently being rendered in the browser and then click debug <strong>Attach to Chrome</strong>. The breakpoint will break on the next re-render of the component and we don't have to navigate on a new browser window.</p>
]]></content:encoded></item></channel></rss>