{"id":1479,"date":"2017-03-21T17:37:31","date_gmt":"2017-03-21T12:07:31","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=1479"},"modified":"2017-03-29T10:51:59","modified_gmt":"2017-03-29T05:21:59","slug":"generate-random-integers-within-specific-range-java","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/generate-random-integers-within-specific-range-java\/","title":{"rendered":"JAVA &#8211; How to generate random integers within a specific range in Java"},"content":{"rendered":"<ul>\n<li>In programming world, we often need to generate random numbers, sometimes random integers in a range e.g. 1 to 100 etc.<\/li>\n<li>Random number generation in Java is easy as Java API provides good support for random numbers via java.util.Random class, Math.random() utility method and recently ThreadLocalRandom class in Java 7.<\/li>\n<li>random() method is the most convenient way of generating randoms in Java it only returns random doubles, on the other hand by using Random, we can generate pseudo-random integer, floating point numbers e.g. double and even random boolean values.<\/li>\n<\/ul>\n<p><strong>Three ways to generate random integers in a range.<\/strong><\/p>\n<ol>\n<li>java.util.Random.nextInt<\/li>\n<li>Math.random.<\/li>\n<li>java.util.Random.ints (Java 8)<\/li>\n<\/ol>\n<h4 id=\"java-util-random\"><span style=\"color: #ff6600;\"><b>java.util.Random<\/b><\/span><\/h4>\n<ul>\n<li>This Random().nextInt(int bound) generates a random integer from 0 (inclusive) to bound (exclusive).<\/li>\n<\/ul>\n<p>For <b>getRandomNumberInRange(5, 10), <\/b>this will generates a random integer between 5 (inclusive) and 10 (inclusive).<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">private static int getRandomNumberInRange(int min, int max) <br\/>{<br\/><br\/>\tif (min &gt;= max) <br\/>{<br\/>\t\tthrow new IllegalArgumentException(&quot;max must be greater than min&quot;);<br\/>}<br\/><br\/>\tRandom r = new Random();<br\/>\treturn r.nextInt((max - min) + 1) + min;<br\/>}<\/code><\/pre> <\/div>\n<p><b>What is (max \u2013 min) + 1) + min?<\/b><\/p>\n<p>Above formula will generates a random integer in a range between min (inclusive) and max (inclusive).<\/p>\n<p><strong>\/\/1. nextInt(range) = nextInt(max &#8211; min)<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/Random().nextInt(int bound) = Random integer from 0 (inclusive) to bound (exclusive)<br\/><br\/><br\/>new Random().nextInt(5);  \/\/ [0...4] [min = 0, max = 4]<br\/>new Random().nextInt(6);  \/\/ [0...5]<br\/>new Random().nextInt(7);  \/\/ [0...6]<br\/>new Random().nextInt(8);  \/\/ [0...7]<br\/>new Random().nextInt(9);  \/\/ [0...8]<br\/>new Random().nextInt(10); \/\/ [0...9]<br\/>new Random().nextInt(11); \/\/ [0...10]<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<p><strong>\/\/2. To include the last value (max value) = (range + 1)<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">new Random().nextInt(5 + 1)  \/\/ [0...5] [min = 0, max = 5]<br\/>new Random().nextInt(6 + 1)  \/\/ [0...6]<br\/>new Random().nextInt(7 + 1)  \/\/ [0...7]<br\/>new Random().nextInt(8 + 1)  \/\/ [0...8]<br\/>new Random().nextInt(9 + 1)  \/\/ [0...9]<br\/>new Random().nextInt(10 + 1) \/\/ [0...10]<br\/>new Random().nextInt(11 + 1) \/\/ [0...11]<\/code><\/pre> <\/div>\n<p><strong>\/\/3. To define a start value (min value) in a range,<\/strong><\/p>\n<p><strong>\/\/\u00a0\u00a0 For example, the range should start from 10 = (range + 1) + min<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">new Random().nextInt(5 + 1)  + 10 \/\/ [0...5]  + 10 = [10...15]<br\/>new Random().nextInt(6 + 1)  + 10 \/\/ [0...6]  + 10 = [10...16]<br\/>new Random().nextInt(7 + 1)  + 10 \/\/ [0...7]  + 10 = [10...17]<br\/>new Random().nextInt(8 + 1)  + 10 \/\/ [0...8]  + 10 = [10...18]<br\/>new Random().nextInt(9 + 1)  + 10 \/\/ [0...9]  + 10 = [10...19]<br\/>new Random().nextInt(10 + 1) + 10 \/\/ [0...10] + 10 = [10...20]<br\/>new Random().nextInt(11 + 1) + 10 \/\/ [0...11] + 10 = [10...21]<\/code><\/pre> <\/div>\n<p><strong>\/\/ Range = (max &#8211; min)<\/strong><\/p>\n<p><strong>\/\/ So, the final formula is ((max &#8211; min) + 1) + min<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p><strong>\/\/4. Test [10&#8230;30]<\/strong><\/p>\n<p><strong>\/\/ min = 10 , max = 30, range = (max &#8211; min)<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">new Random().nextInt((max - min) + 1) + min<br\/>new Random().nextInt((30 - 10) + 1) + 10<br\/>new Random().nextInt((20) + 1) + 10<br\/>new Random().nextInt(21) + 10    \/\/[0...20] + 10 = [10...30]<\/code><\/pre> <\/div>\n<p><strong>\/\/5. Test [15&#8230;99]<\/strong><\/p>\n<p><strong>\/\/ min = 15 , max = 99, range = (max &#8211; min)<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">new Random().nextInt((max - min) + 1) + min<br\/>new Random().nextInt((99 - 15) + 1) + 15<br\/>new Random().nextInt((84) + 1) + 15<br\/>new Random().nextInt(85) + 15    \/\/[0...84] + 15 = [15...99]<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"example\"><span style=\"color: #800080;\"><b>Example:<\/b><\/span><\/h4>\n<p>To generate 10 random integers in a range between 5 (inclusive) and 10 (inclusive).<\/p>\n<p>WikitechyRandom.java<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">package com.wiki.example.test;<br\/>import java.util.Random;<br\/>public class WikitechyRandom <br\/>{<br\/> static void main(String[] args) <br\/>{<br\/>for (int i = 0; i &lt; 10; i++) <br\/>{<br\/>System.out.println(getRandomNumberInRange(5, 10));<br\/>}<br\/>}<br\/>private static int getRandomNumberInRange(int min, int max)<br\/>{<br\/>if (min &gt;= max) <br\/>{<br\/>throw new IllegalArgumentException(&quot;max must be greater than min&quot;);<br\/>}<br\/>Random r = new Random();<br\/>return r.nextInt((max - min) + 1) + min;<br\/>}<br\/>}<br\/>private static int getRandomNumberInRange(int min, int max) <br\/>{<br\/>if (min &gt;= max) <br\/>{<br\/>throw new IllegalArgumentException(&quot;max must be greater than min&quot;);<br\/>}<br\/>Random r = new Random();<br\/>return r.nextInt((max - min) + 1) + min;<br\/>}<br\/>}<br\/>private static int getRandomNumberInRange(int min, int max) <br\/>{<br\/>if (min &gt;= max) <br\/>{<br\/>throw new IllegalArgumentException(&quot;max must be greater than min&quot;);<br\/>}<br\/>Random r = new Random();<br\/>return r.nextInt((max - min) + 1) + min;<br\/>}<br\/>}<br\/>private static int getRandomNumberInRange(int min, int max) {<br\/>if (min &gt;= max) <br\/>{<br\/>throw new IllegalArgumentException(&quot;max must be greater than min&quot;);<br\/>}<br\/>Random r = new Random();<br\/>return r.nextInt((max - min) + 1) + min;<br\/>}<br\/>}<\/code><\/pre> <\/div>\n<h4 id=\"output\"><strong><span style=\"color: #808000;\">Output.<\/span><\/strong><\/h4>\n<ul>\n<li style=\"text-align: left;\">7<\/li>\n<li style=\"text-align: left;\">6<\/li>\n<li style=\"text-align: left;\">10<\/li>\n<li style=\"text-align: left;\">8<\/li>\n<li style=\"text-align: left;\">9<\/li>\n<li style=\"text-align: left;\">5<\/li>\n<li style=\"text-align: left;\">7<\/li>\n<li style=\"text-align: left;\">10<\/li>\n<li style=\"text-align: left;\">8<\/li>\n<li style=\"text-align: left;\">5<\/li>\n<\/ul>\n<h4 id=\"math-random\"><span style=\"color: #808000;\"><b>Math.random<\/b><\/span><\/h4>\n<ul>\n<li>This Math.random() gives a random double from 0.0 (inclusive) to 1.0 (exclusive).<\/li>\n<\/ul>\n<h4 id=\"formula\"><span style=\"color: #ff6600;\"><b>Formula:<\/b><\/span><\/h4>\n<p><strong>int)(Math.random() * ((max &#8211; min) + 1)) + min<\/strong><\/p>\n<h4 id=\"example-2\"><b>Example: <\/b><\/h4>\n<ul>\n<li>To generate 10 random integers in a range between 26 (inclusive) and 30 (inclusive).<\/li>\n<\/ul>\n<h4 id=\"wikitechyrandom-java\"><span style=\"color: #800080;\"><b>WikitechyRandom.java<\/b><\/span><\/h4>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">package com.wiki.example.test;<br\/>public class WikitechyRandom <br\/>{<br\/><br\/>\tpublic static void main(String[] args) <br\/>{<br\/><br\/>\t\tfor (int i = 0; i &lt; 10; i++) <br\/>{<br\/>\t\t\tSystem.out.println(getRandomNumberInRange(26, 30));<br\/>}<br\/><br\/>}<br\/>private static int getRandomNumberInRange(int min, int max) <br\/>{<br\/><br\/>\t\tif (min &gt;= max) <br\/>{<br\/>\t\t\tthrow new IllegalArgumentException(&quot;max must be greater than min&quot;);<br\/>}<br\/><br\/>\t\treturn (int)(Math.random() * ((max - min) + 1)) + min;<br\/>}<br\/><br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n<h4 id=\"output-2\"><span style=\"color: #808000;\"><strong>Output.<\/strong><\/span><\/h4>\n<ul>\n<li>27<\/li>\n<li>26<\/li>\n<li>30<\/li>\n<li>29<\/li>\n<li>30<\/li>\n<li>30<\/li>\n<li>30<\/li>\n<li>27<\/li>\n<li>30<\/li>\n<li>26<\/li>\n<\/ul>\n<h4 id=\"note\"><span style=\"color: #333300;\"><strong>Note:<\/strong><\/span><\/h4>\n<ul>\n<li>The\u00a0Random.nextInt(n)\u00a0is more efficient than\u00a0Math.random() * n<\/li>\n<\/ul>\n<h4 id=\"java-util-random-ints\"><span style=\"color: #ff6600;\"><b>java.util.Random.ints<\/b><\/span><\/h4>\n<ul>\n<li>In Java 8, New methods are added in java.util.Random<\/li>\n<\/ul>\n<p><strong>public IntStream ints(int randomNumberOrigin, int randomNumberBound)<\/strong><\/p>\n<p><strong>public IntStream ints(long streamSize, int randomNumberOrigin, int randomNumberBound)<\/strong><\/p>\n<ul>\n<li>This Random.ints(int origin, int bound) or Random.ints(int min, int max) generates a random integer from origin (inclusive) to bound (exclusive).<\/li>\n<\/ul>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">private static int getRandomNumberInRange(int min, int max) <br\/>{<br\/><br\/>\tRandom r = new Random();<br\/>\treturn r.ints(min, (max + 1)).findFirst().getAsInt();<br\/><br\/>}<\/code><\/pre> <\/div>\n<h4 id=\"example-3\"><span style=\"color: #800080;\"><b>Example :<\/b><\/span><\/h4>\n<ul>\n<li><strong>To generate 10 random integers in a range between 43 (inclusive) and 48 (inclusive).<\/strong><\/li>\n<\/ul>\n<p><strong>WikitechyRandom.java<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">java code<\/span> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">package com.wiki.form.test;<br\/><br\/>import java.util.Random;<br\/>public class WikitechyRandom <br\/>{<br\/>public static void main(String[] args) <br\/>{<br\/>for (int i = 0; i &lt; 10; i++) <br\/>{<br\/>System.out.println(getRandomNumberInRange(43, 48));<br\/>}<br\/>}<br\/>private static int getRandomNumberInRange(int min, int max) <br\/>{<br\/>Random r = new Random();<br\/>return r.ints(min, (max + 1)).limit(1).findFirst().getAsInt();<br\/>}<br\/>}<\/code><\/pre> <\/div>\n<h4 id=\"output-3\"><span style=\"color: #808000;\"><strong>Output.<\/strong><\/span><\/h4>\n<ul>\n<li>44<\/li>\n<li>45<\/li>\n<li>47<\/li>\n<li>43<\/li>\n<li>48<\/li>\n<li>47<\/li>\n<li>44<\/li>\n<li>45<\/li>\n<li>46<\/li>\n<li>47<\/li>\n<\/ul>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>In programming world, we often need to generate random numbers, sometimes random integers in a range e.g. 1 to 100 etc. Random number generation in Java is easy as Java API provides good support for random numbers via java.util.Random class, Math.random() utility method and recently ThreadLocalRandom class in Java 7. random() method is the most [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2139],"tags":[2940,2953,2938,2954,2941,2942,2939,2937,2943,2946,2950,2947,2949,2952,2944,2945,2951,2948],"class_list":["post-1479","post","type-post","status-publish","format-standard","hentry","category-java","tag-generating-random-integer-from-a-range","tag-generating-random-integers-within-range-with-a-probability-distribution","tag-generating-random-whole-numbers-in-javascript-in-a-specific-range","tag-getting-a-range-off-user-input-for-random-generation","tag-getting-random-numbers-in-java","tag-how-can-i-generate-random-number-in-specific-range-in-android","tag-how-do-i-generate-a-random-int-number-in-c","tag-how-to-generate-a-random-alpha-numeric-string","tag-how-to-generate-a-random-integer-in-the-range-0","tag-how-to-generate-random-numbers-in-java-within-range","tag-java-random-double","tag-java-random-number-between-0-and-1","tag-java-random-number-between-0-and-10","tag-java-random-seed","tag-n-from-a-stream-of-random-bits-without-wasting-bits","tag-problems-generating-random-numbers-within-a-range-with-java","tag-random-function-in-java","tag-random-number-generator-java-without-repetition-random-nextint-java"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=1479"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/1479\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=1479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=1479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=1479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}