<?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>DEV Community: Shunya</title>
    <description>The latest articles on DEV Community by Shunya (@shunya).</description>
    <link>https://dev.to/shunya</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1087489%2F4e88b491-5034-4870-9d21-301a47c737c6.png</url>
      <title>DEV Community: Shunya</title>
      <link>https://dev.to/shunya</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shunya"/>
    <language>en</language>
    <item>
      <title>Javascript 101: Generating a Random Number</title>
      <dc:creator>Shunya</dc:creator>
      <pubDate>Mon, 22 May 2023 20:19:22 +0000</pubDate>
      <link>https://dev.to/shunya/javascript-101-generating-a-random-number-2f77</link>
      <guid>https://dev.to/shunya/javascript-101-generating-a-random-number-2f77</guid>
      <description>&lt;p&gt;Hey there, fellow coders and tech enthusiasts! Today, we’re diving into the wonderful world of coding snippets. 🚀&lt;/p&gt;

&lt;p&gt;If you’ve spent any time in the vast realm of programming, you’ve likely come across those bite-sized chunks of code that very quickly solve common problems or add nifty functionalities to your projects. We’re talking about those trusty companions that make our coding journey smoother and more efficient.&lt;/p&gt;

&lt;p&gt;Here we’ll be exploring some of the most commonly used coding snippets, that you might require one day, whether you’re a seasoned pro or just starting your coding adventure. These snippets will surely come in handy and save you precious time and effort.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Generating a Random Number&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getRandomNumber(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

// Usage example
const randomNumber = getRandomNumber(1, 100);
console.log(randomNumber); // Output: Random number between 1 and 100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here I have used a built-in function called &lt;code&gt;Math.random()&lt;/code&gt; that generates a pseudo-random decimal number between 0 (inclusive) and 1 (exclusive)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.Random()
// 0.6252474231132967

Math.Random()
// 0.03772396106405407
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And when we multiply it with an integer N, it scales the range of the generated random number to be between 0 (inclusive) and N(exclusive).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.random()*8
// 2.27924218449437

Math.random()*80
// 13.722102542882197
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;(max - min + 1)&lt;/code&gt;, calculates the range of possible values by subtracting the minimum value (&lt;code&gt;min&lt;/code&gt;) from the maximum value (&lt;code&gt;max&lt;/code&gt;) and adding 1(to include the upper bound). You can drop the 1 if you want to exclude the upper bound.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(5 - 0 + 1)
// 6 ==&amp;gt; Math.random()*6 will generate in range (0-5)

/* Without the 1 */
(5 - 0)
// 5 ==&amp;gt; Math.random()*5 will be in range (0-4)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;Math.floor()&lt;/code&gt; function is used to round down the decimal number obtained in the previous step to the nearest integer. This ensures that the final result will be an integer.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Math.random()*5
// Will look like - 1.359376633672147

/* We need to drop the decimal part*/
Math.floor(Math.random()*5)
// 1 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;Math.floor(Math.random() * (max - min + 1)) + min&lt;/code&gt;: Lastly, by adding the minimum value (&lt;code&gt;min&lt;/code&gt;) to the randomly generated integer, we shift the range to start from the minimum value and end at the maximum value. This results in a random integer within the desired range, inclusive of both the minimum and maximum values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/* min=0, max=5 */
Math.floor(Math.random()*5)
// 2 || 3 || 0

/* To generate in a range (5-10) */
Math.floor(Math.random()*(10 - 5 + 1)) + 5
// 6 || 8 || 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this post, we covered generating random numbers. Remember to adapt and customize the code to suit your specific requirements. JavaScript’s versatility and these code snippets will undoubtedly elevate your coding prowess and productivity.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>programming</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
