<?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" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        
        <title>
            <![CDATA[ random - freeCodeCamp.org ]]>
        </title>
        <description>
            <![CDATA[ Browse thousands of programming tutorials written by experts. Learn Web Development, Data Science, DevOps, Security, and get developer career advice. ]]>
        </description>
        <link>https://www.freecodecamp.org/news/</link>
        <image>
            <url>https://cdn.freecodecamp.org/universal/favicons/favicon.png</url>
            <title>
                <![CDATA[ random - freeCodeCamp.org ]]>
            </title>
            <link>https://www.freecodecamp.org/news/</link>
        </image>
        <generator>Eleventy</generator>
        <lastBuildDate>Fri, 26 Jun 2026 22:47:53 +0000</lastBuildDate>
        <atom:link href="https://www.freecodecamp.org/news/tag/random/rss.xml" rel="self" type="application/rss+xml" />
        <ttl>60</ttl>
        
            <item>
                <title>
                    <![CDATA[ RNG Meaning – What does RNG Stand for in Gaming? ]]>
                </title>
                <description>
                    <![CDATA[ If everything is predictable in a game, that isn't much fun. RNGs, or Random Number Generators, are a way to introduce a touch of randomness and causality you need to spice it up. In this article, we'll learn how random number generators work. How an... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/rng-meaning-what-does-rng-stand-for-in-gaming/</link>
                <guid isPermaLink="false">66b0c3ab4d2b90ec4a447a58</guid>
                
                    <category>
                        <![CDATA[ #Game Design ]]>
                    </category>
                
                    <category>
                        <![CDATA[ gaming ]]>
                    </category>
                
                    <category>
                        <![CDATA[ JavaScript ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Math ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Python ]]>
                    </category>
                
                    <category>
                        <![CDATA[ random ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ Ilenia Magoni ]]>
                </dc:creator>
                <pubDate>Wed, 15 Sep 2021 21:55:10 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2021/09/pexels-pixabay-37534.jpg" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>If everything is predictable in a game, that isn't much fun. RNGs, or Random Number Generators, are a way to introduce a touch of randomness and causality you need to spice it up.</p>
<p>In this article, we'll learn how random number generators work.</p>
<h2 id="heading-how-an-analogic-random-number-generator-works">How an Analogic Random Number Generator Works</h2>
<p>The simplest form of a RNG is throwing dice or flipping coins.</p>
<p>Using a single die or coin means that each value has the same probability of occurring. Using multiple dice or coins instead will give a lower probability to the highest and lower values, and a higher probability to the middle values.</p>
<p>The oldest known tabletop game, <a target="_blank" href="https://en.wikipedia.org/wiki/Royal_Game_of_Ur">the Royal Game of Ur</a>, uses four 4-sided dice. Each die can give a value of 0 or 1 meaning that the value obtained by a single dice throw can go from 0 to 4.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/image-1.png" alt="Image" width="600" height="400" loading="lazy">
<em>All the possible combinations obtained by throwing 4 dice, each can give a value of 0 or 1</em></p>
<p>There are 16 possible combinations, of which one gives a value of 0, 4 gives a value of 1, 6 gives a value of 2, 4 gives a value of 3, and one gives a value of 4.</p>
<p>In this case there is a 1/16 or 6.25% chance of getting 0, 1/4 or 25% chance of getting 1, 3/8 or 37.5% chance of getting 2, 1/4 or 25% chance of getting 3 and 1/16 or 6.25% change of getting 4.</p>
<p>More complex games have manuals full of tables to determine something randomly.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/image-2.png" alt="Image" width="600" height="400" loading="lazy">
<em>Part of a table for random effects after drinking a potion. <a target="_blank" href="https://luetkemj.github.io/160419/random-potion-effects-table">Here's the whole table</a>.</em></p>
<p>Any game that uses dice has an analogic random number generator.</p>
<h2 id="heading-how-random-number-generators-work-in-video-games">How Random Number Generators Work in Video Games</h2>
<p>In video games, RNGs are much less noticeable and more complex, and players might not even be aware they exist. <a target="_blank" href="https://www.freecodecamp.org/news/random-number-generator/">There are many ways you can generate a Random Number</a>, but how do you actually use one?</p>
<p>Breaking it down into the simplest terms, using a RNG is not dissimilar from what you saw above with the dice throw used to determine an effect from a table. You just don't see the dice throw.</p>
<p>In a video game, you can use a RNG to determine what kind of loot might be dropped by a fallen enemy, or what you can find in a chest, or what kind of random encounter will await you, or even what the weather will be.</p>
<p>RNGs are used, for example, to live up open world games without the developers having to code every single section of forests and roads and deserts. Instead, developers code some possibilities and let chance determine what happens when the player reaches a certain point in the map. </p>
<p>Will you meet a bear, a wolf pack, or some bandits? The game does its version of rolling a die to determine that.</p>
<p>Let's see how to code a simple example of a Random Number Generator to better understand how they actually work.</p>
<h2 id="heading-how-to-code-a-random-number-generator">How to Code a Random Number Generator</h2>
<p>Most programming languages contain a <code>random</code> function. This function returns a random number, and what kind of random number depends on its implementation.</p>
<p>For example, in <a target="_blank" href="https://www.freecodecamp.org/news/javascript-math-random-method-explained/">JavaScript</a>, <a target="_blank" href="https://www.freecodecamp.org/news/javascript-math-random-method-explained/"><code>Math.random()</code></a> returns a random number between 0 (included) and 1 (not included). In Python, <code>randint</code> from the <code>random</code> module returns a whole number in a range (Python has also a function that does the same as JavaScript's <code>Math.random</code>).</p>
<p>Let's consider a pretty common video game situation: we have an enemy that often drops a common item, but now and then drops something rare. This enemy may be, for example, a wolf that could drop a wolf pelt (common) or a wolf fang (rare). </p>
<p>How do you determine what is "rare"? That depends on you – it can be that 1 in 10 drops is a rare item, or that 1 in 100 drops is a rare item. A middle ground may be a chance of 1 in 25 for a rare items. And to complicate it a bit, there could be also a 1 in 10 chance of no item.</p>
<p>In this case you would need a function that returns a value between 0 and 1.</p>
<p>A chance of 1 in 25 is 4%, and a chance of 1 in 10 is 10%. In decimal form that would be 0.04 and 0.1, respectively. </p>
<p>In this case you can say that a number in the range from 0 to 0.04 gives the rare item, and a number in the range from 0.9 to 1 gives no item.</p>
<p><img src="https://www.freecodecamp.org/news/content/images/2021/09/image-3.png" alt="Image" width="600" height="400" loading="lazy">
<em>The percentage breakdown of the wolf drop</em></p>
<p>To avoid sticking to one language, let's first see how we can code this using <a target="_blank" href="https://www.freecodecamp.org/news/what-is-pseudocode-in-programming/">pseudocode</a>. This is not a real programming language – rather, it's a way to break down the code logic. It's like taking notes, as it's personal and will have varied syntax depending on the person writing it.</p>
<pre><code>FUNCTION wolfDrop
  randomNumber = random(<span class="hljs-number">0</span>,<span class="hljs-number">1</span>)
  IF
    randomNumber &lt; <span class="hljs-number">0.04</span>
    THEN
     -&gt; wolf fang
  ELSE IF
    randomNumber &lt; <span class="hljs-number">0.9</span>
    THEN
     -&gt; wolf pelt
  ELSE
    -&gt; empty
  END IF
END FUNCTION
</code></pre><p>Or a more verbose version:</p>
<blockquote>
<p>Create a function called <code>wolfDrop</code> and inside it store a random number between 0 (included) and 1 (excluded) in the <code>randomNumber</code> variable. If <code>randomNumber</code> has a value less than <code>0.04</code> the drop will be a wolf fang, else if the <code>randomNumber</code> has a value less than <code>0.9</code> the drop will be a wolf pelt, and otherwise there will be no drop.</p>
</blockquote>
<p>With the pseudocode ready, we can implement the code snippet in any language. Let's see, for example, how to code it in a few different languages:</p>
<pre><code class="lang-javascript"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">wolfDrop</span> (<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">const</span> randomNumber = <span class="hljs-built_in">Math</span>.random();
  <span class="hljs-keyword">if</span> (randomNumber &lt; <span class="hljs-number">0.04</span>) {
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Wolf fang"</span>;
  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (randomNumber &lt; <span class="hljs-number">0.9</span>) {
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Wolf pelt"</span>;
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-keyword">return</span>;
  }
}
</code></pre>
<pre><code class="lang-python"><span class="hljs-keyword">import</span> random
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wolfDrop</span>():</span>
  randomNumber = random.random()
  <span class="hljs-keyword">if</span> randomNumber &lt; <span class="hljs-number">0.04</span>:
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Wolf fang"</span>
  <span class="hljs-keyword">elif</span> randomNumber &lt; <span class="hljs-number">0.9</span>:
    <span class="hljs-keyword">return</span> <span class="hljs-string">"Wolf pelt"</span>
  <span class="hljs-keyword">else</span>
    <span class="hljs-keyword">return</span>
</code></pre>
<pre><code class="lang-clojure">(<span class="hljs-keyword">defn</span> <span class="hljs-title">wolf-drop</span> []
  (<span class="hljs-name"><span class="hljs-builtin-name">let</span></span> [random-number (<span class="hljs-name"><span class="hljs-builtin-name">rand</span></span>)]
    (<span class="hljs-name"><span class="hljs-builtin-name">cond</span></span> (<span class="hljs-name"><span class="hljs-builtin-name">&lt;</span></span> random-number <span class="hljs-number">0.04</span>) <span class="hljs-string">"Wolf fang"</span>
          (<span class="hljs-name"><span class="hljs-builtin-name">&lt;</span></span> random-number <span class="hljs-number">0.9</span>) <span class="hljs-string">"Wolf pelt"</span>)))
</code></pre>
<pre><code class="lang-go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">wolfDrop</span><span class="hljs-params">()</span> <span class="hljs-title">string</span></span> {
    randomNumber := rand.Float64()
    <span class="hljs-keyword">switch</span> {
        <span class="hljs-keyword">case</span> randomNumber &lt; <span class="hljs-number">0.04</span>:
            <span class="hljs-keyword">return</span> <span class="hljs-string">"Wolf fang"</span>
        <span class="hljs-keyword">case</span> randomNumber &lt; <span class="hljs-number">0.9</span>:
            <span class="hljs-keyword">return</span> <span class="hljs-string">"Wolf pelt"</span>
        <span class="hljs-keyword">default</span>:
            <span class="hljs-keyword">return</span> <span class="hljs-string">""</span>
    }
}
</code></pre>
<pre><code class="lang-kotlin"><span class="hljs-function"><span class="hljs-keyword">fun</span> <span class="hljs-title">wolfDrop</span><span class="hljs-params">()</span></span>: String {
    <span class="hljs-keyword">val</span> randomNumber = Random.nextFloat()
    <span class="hljs-keyword">when</span> {
        randomNumber &lt; <span class="hljs-number">0.04</span> -&gt; <span class="hljs-keyword">return</span> <span class="hljs-string">"Wolf fang"</span>
        randomNumber &lt; <span class="hljs-number">0.9</span> -&gt; <span class="hljs-keyword">return</span> <span class="hljs-string">"Wolf pelt"</span>
        <span class="hljs-keyword">else</span> -&gt; <span class="hljs-keyword">return</span> <span class="hljs-string">""</span>
    }
}
</code></pre>
<pre><code class="lang-elixir"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">wolf_pelt</span></span>() <span class="hljs-keyword">do</span>
  random_number = <span class="hljs-symbol">:rand</span>.uniform()
  <span class="hljs-keyword">cond</span> <span class="hljs-keyword">do</span>
    random_number &lt; 0.04 -&gt; <span class="hljs-string">"Wolf fang"</span>
    random_number &lt; 0.<span class="hljs-number">9</span> -&gt; <span class="hljs-string">"Wolf pelt"</span>
    <span class="hljs-keyword">true</span> -&gt; <span class="hljs-keyword">nil</span>
  <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<pre><code class="lang-c#"><span class="hljs-function"><span class="hljs-keyword">string</span> <span class="hljs-title">WolfPelt</span>(<span class="hljs-params"></span>)</span> {
  <span class="hljs-keyword">var</span> random = <span class="hljs-keyword">new</span> Random();
  <span class="hljs-keyword">double</span> randomNumber = random.NextDouble();
  <span class="hljs-keyword">if</span> (randomNumber &lt; <span class="hljs-number">0.04</span>) <span class="hljs-keyword">return</span> <span class="hljs-string">"Wolf fang"</span>;
  <span class="hljs-keyword">if</span> (randomNumber &lt; <span class="hljs-number">0.9</span>) <span class="hljs-keyword">return</span> <span class="hljs-string">"Wolf pelt"</span>;
  <span class="hljs-keyword">return</span> <span class="hljs-literal">null</span>;
}
</code></pre>
<pre><code class="lang-rust"><span class="hljs-keyword">extern</span> <span class="hljs-keyword">crate</span> rand;

<span class="hljs-function"><span class="hljs-keyword">fn</span> <span class="hljs-title">wolf_drop</span></span>() -&gt; &amp;<span class="hljs-symbol">'static</span> <span class="hljs-built_in">str</span> {
  <span class="hljs-keyword">let</span> random_number: <span class="hljs-built_in">f64</span> = rand::random();
  <span class="hljs-keyword">if</span> random_number &lt; <span class="hljs-number">0.04</span> {
    <span class="hljs-string">"Wolf fang"</span>
  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> random_number &lt; <span class="hljs-number">0.9</span> {
    <span class="hljs-string">"Wolf pelt"</span>
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-string">""</span>
  }
}
</code></pre>
<pre><code class="lang-c"><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;stdlib.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;string.h&gt;</span></span>
<span class="hljs-meta">#<span class="hljs-meta-keyword">include</span> <span class="hljs-meta-string">&lt;time.h&gt;</span></span>

<span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">wolf_drop</span><span class="hljs-params">(<span class="hljs-keyword">char</span> *drop_item)</span> </span>{
  srand((<span class="hljs-keyword">unsigned</span>) time(<span class="hljs-number">0</span>));
  <span class="hljs-keyword">double</span> random_number = <span class="hljs-number">1.0</span> * rand() / RAND_MAX;
  <span class="hljs-keyword">if</span> (random_number &lt; <span class="hljs-number">0.04</span>) {
    <span class="hljs-built_in">strncpy</span>(drop_item, <span class="hljs-string">"wolf fang\0"</span>, <span class="hljs-number">10</span>);
  } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (random_number &lt; <span class="hljs-number">0.9</span>) {
    <span class="hljs-built_in">strncpy</span>(drop_item, <span class="hljs-string">"wolf pelt\0"</span>, <span class="hljs-number">10</span>);
  } <span class="hljs-keyword">else</span> {
    <span class="hljs-built_in">strncpy</span>(drop_item, <span class="hljs-string">"\0"</span>, <span class="hljs-number">1</span>);
  }
  <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;
}
</code></pre>
<pre><code class="lang-julia"><span class="hljs-keyword">function</span> wolfdrop()
    randomnumber = rand()
    <span class="hljs-keyword">if</span> randomnumber &lt; <span class="hljs-number">0.04</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"wolf fang"</span>
    <span class="hljs-keyword">elseif</span> randomnumber &lt; <span class="hljs-number">0.9</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">"wolf pelt"</span>
    <span class="hljs-keyword">else</span>
        <span class="hljs-keyword">return</span> <span class="hljs-string">""</span>
    <span class="hljs-keyword">end</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p>(Thanks to <a target="_blank" href="https://forum.freecodecamp.org/u/alpox">alpox</a> for the code snippets in Clojure, Golang, Kotlin, Elixir and C#, and to <a target="_blank" href="https://www.freecodecamp.org/news/author/jeremylt/">Jeremy</a> for the snippets in Rust, C, and Julia.)</p>
<h3 id="heading-other-examples-of-mathrandom">Other examples of math.random</h3>
<p>If you want to learn more about all this, you can read this article about the <a target="_blank" href="https://www.freecodecamp.org/news/how-to-use-javascript-math-random-as-a-random-number-generator/">Math.random function in JavaScript</a> and create a Dice Rolling Game. </p>
<p>You can also read this <a target="_blank" href="https://www.freecodecamp.org/news/how-to-make-your-own-procedural-dungeon-map-generator-using-the-random-walk-algorithm-e0085c8aa9a/">article on using the random walk algorithm</a> and create a random dungeon map with JavaScript to experiment some more with RNGs.</p>
<h2 id="heading-conclusion">Conclusion</h2>
<p>Random Number Generators, or RNGs, are used in many games. In this article, you have learned how and why they are used, and you've seen an example implementation. </p>
<p>Next time you play a video game, will you be able to spot where a RNG may be used?</p>
 ]]>
                </content:encoded>
            </item>
        
            <item>
                <title>
                    <![CDATA[ Java Random Number Generator – How to Generate Integers With Math Random ]]>
                </title>
                <description>
                    <![CDATA[ By Thanoshan MV Computer generated random numbers are divided into two categories: true random numbers and pseudo-random numbers.  True random numbers are generated based on external factors. For example, generating randomness using surrounding noise... ]]>
                </description>
                <link>https://www.freecodecamp.org/news/generate-random-numbers-java/</link>
                <guid isPermaLink="false">66d46149c7632f8bfbf1e4c9</guid>
                
                    <category>
                        <![CDATA[ Java ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Math ]]>
                    </category>
                
                    <category>
                        <![CDATA[ Mathematics ]]>
                    </category>
                
                    <category>
                        <![CDATA[ random ]]>
                    </category>
                
                <dc:creator>
                    <![CDATA[ freeCodeCamp ]]>
                </dc:creator>
                <pubDate>Wed, 25 Nov 2020 22:50:46 +0000</pubDate>
                <media:content url="https://www.freecodecamp.org/news/content/images/2020/11/Untitled-design--1-.png" medium="image" />
                <content:encoded>
                    <![CDATA[ <p>By Thanoshan MV</p>
<p>Computer generated random numbers are divided into two categories: true random numbers and pseudo-random numbers. </p>
<p>True random numbers are generated based on external factors. For example, generating randomness using surrounding noises. </p>
<p>But generating such true random number is a time consuming task. Therefore, we can utilize pseudo-random numbers which are generated using an algorithm and a seed value. </p>
<p>These pseudo-random numbers are sufficient for most purposes. For example, you can use them in cryptography, in building games such as dice or cards, and in generating OTP (one-time password) numbers. </p>
<p>In this article, we will learn how to generate pseudo-random numbers using <code>Math.random()</code> in Java. </p>
<h2 id="heading-1-use-mathrandom-to-generate-integers">1. Use Math.random() to Generate Integers</h2>
<p><code>Math.random()</code> returns a double type pseudo-random number, greater than or equal to zero and less than one. </p>
<p>Let's try it out with some code:</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-keyword">double</span> randomNumber = Math.random();
        System.out.println(randomNumber);
    }
    <span class="hljs-comment">// output #1 = 0.5600740702032417</span>
    <span class="hljs-comment">// output #2 = 0.04906751303932033</span>
</code></pre>
<p><code>randomNumber</code> will give us a different random number for each execution. </p>
<p>Let's say we want to generate random numbers within a specified range, for example, zero to four. </p>
<pre><code class="lang-java">    <span class="hljs-comment">// generate random numbers between 0 to 4</span>
    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-comment">// Math.random() generates random number from 0.0 to 0.999</span>
        <span class="hljs-comment">// Hence, Math.random()*5 will be from 0.0 to 4.999</span>
        <span class="hljs-keyword">double</span> doubleRandomNumber = Math.random() * <span class="hljs-number">5</span>;
        System.out.println(<span class="hljs-string">"doubleRandomNumber = "</span> + doubleRandomNumber);
        <span class="hljs-comment">// cast the double to whole number</span>
        <span class="hljs-keyword">int</span> randomNumber = (<span class="hljs-keyword">int</span>)doubleRandomNumber;
        System.out.println(<span class="hljs-string">"randomNumber = "</span> + randomNumber);
    }
    <span class="hljs-comment">/* Output #1
    doubleRandomNumber = 2.431392914284627
    randomNumber = 2
    */</span>
</code></pre>
<p>When we cast a double to int, the int value keeps only whole number part. </p>
<p>For example, in the above code, <code>doubleRandomNumber</code> is <code>2.431392914284627</code> . <code>doubleRandomNumber</code>'s whole number part is <code>2</code> and fractional part (numbers after the decimal point) is <code>431392914284627</code> . So, <code>randomNumber</code> will only hold the whole number part <code>2</code>. </p>
<p>You can read more about the <code>Math.random()</code> method in the <a target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html">Java documentation</a>. </p>
<p>Using <code>Math.random()</code> is not the only way to generate random numbers in Java. Next, we'll consider how we can generate random numbers using the Random class. </p>
<h2 id="heading-2-use-the-random-class-to-generate-integers">2. Use the Random Class to Generate Integers</h2>
<p>In the Random class, we have many instance methods which provide random numbers. In this section, we will consider two instance methods, <code>nextInt(int bound)</code>, and <code>nextDouble()</code>. </p>
<h3 id="heading-how-to-use-the-nextintint-bound-method">How to use the nextInt(int bound) method</h3>
<p><code>nextInt(int bound)</code> returns an int type pseudo-random number, greater than or equal to zero and less than the bound value. </p>
<p>The <code>bound</code> parameter specifies the range. For example, if we specify the bound as 4, <code>nextInt(4)</code> will return an int type value, greater than or equal to zero and less than four. 0,1,2,3 are the possible outcomes of <code>nextInt(4)</code> .</p>
<p>As this is an instance method we should create a random object to access this method. Let's try it.</p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-comment">// create Random object</span>
        Random random = <span class="hljs-keyword">new</span> Random();
        <span class="hljs-comment">// generate random number from 0 to 3</span>
        <span class="hljs-keyword">int</span> number = random.nextInt(<span class="hljs-number">4</span>);
        System.out.println(number);
    }
</code></pre>
<h3 id="heading-how-to-use-the-nextdouble-method">How to use the nextDouble() method</h3>
<p>Similar to <code>Math.random()</code> , the <code>nextDouble()</code> returns a double type pseudo-random number, greater than or equal to zero and less than one. </p>
<pre><code class="lang-java">    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>{
        <span class="hljs-comment">// create Random object</span>
        Random random = <span class="hljs-keyword">new</span> Random();
        <span class="hljs-comment">// generates random number from 0.0 and less than 1.0</span>
        <span class="hljs-keyword">double</span> number = random.nextDouble();
        System.out.println(number);
    }
</code></pre>
<p>For more information, you can read the random class's <a target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/util/Random.html">Java documentation</a>. </p>
<h2 id="heading-so-which-random-number-method-should-you-use">So which random number method should you use?</h2>
<p><code>[Math.random()](https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#random--)</code> <a target="_blank" href="https://docs.oracle.com/javase/8/docs/api/java/lang/Math.html#random--">uses the random class</a>. If we only want double type pseudo-random numbers in our application, then we can use <code>Math.random()</code> . </p>
<p>Otherwise, we can use the random class as it provides various methods to generate pseudo-random numbers in different types such as <code>nextInt()</code>, <code>nextLong()</code>, <code>nextFloat()</code> and <code>nextDouble()</code>. </p>
<p>Thank you for reading.</p>
<p>Photo image by <a target="_blank" href="https://unsplash.com/@brett_jordan?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Brett Jordan</a> on <a target="_blank" href="https://unsplash.com/s/photos/random-numbers?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></p>
<p>You can connect with me on <a target="_blank" href="https://mvthanoshan.medium.com/">Medium</a>.</p>
<p><strong>Happy Coding!</strong></p>
 ]]>
                </content:encoded>
            </item>
        
    </channel>
</rss>
