{"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[pastacode lang=&#8221;java&#8221; manual=&#8221;private%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%20%0A%7B%0A%0A%09if%20(min%20%3E%3D%20max)%20%0A%7B%0A%09%09throw%20new%20IllegalArgumentException(%22max%20must%20be%20greater%20than%20min%22)%3B%0A%7D%0A%0A%09Random%20r%20%3D%20new%20Random()%3B%0A%09return%20r.nextInt((max%20-%20min)%20%2B%201)%20%2B%20min%3B%0A%7D%0A&#8221; message=&#8221;java code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\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[pastacode lang=&#8221;java&#8221; manual=&#8221;%2F%2FRandom().nextInt(int%20bound)%20%3D%20Random%20integer%20from%200%20(inclusive)%20to%20bound%20(exclusive)%0A%0A%0Anew%20Random().nextInt(5)%3B%20%20%2F%2F%20%5B0&#8230;4%5D%20%5Bmin%20%3D%200%2C%20max%20%3D%204%5D%0Anew%20Random().nextInt(6)%3B%20%20%2F%2F%20%5B0&#8230;5%5D%0Anew%20Random().nextInt(7)%3B%20%20%2F%2F%20%5B0&#8230;6%5D%0Anew%20Random().nextInt(8)%3B%20%20%2F%2F%20%5B0&#8230;7%5D%0Anew%20Random().nextInt(9)%3B%20%20%2F%2F%20%5B0&#8230;8%5D%0Anew%20Random().nextInt(10)%3B%20%2F%2F%20%5B0&#8230;9%5D%0Anew%20Random().nextInt(11)%3B%20%2F%2F%20%5B0&#8230;10%5D%0A&#8221; message=&#8221;java code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n[ad type=&#8221;banner&#8221;]\n<p><strong>\/\/2. To include the last value (max value) = (range + 1)<\/strong><\/p>\n[pastacode lang=&#8221;java&#8221; manual=&#8221;new%20Random().nextInt(5%20%2B%201)%20%20%2F%2F%20%5B0&#8230;5%5D%20%5Bmin%20%3D%200%2C%20max%20%3D%205%5D%0Anew%20Random().nextInt(6%20%2B%201)%20%20%2F%2F%20%5B0&#8230;6%5D%0Anew%20Random().nextInt(7%20%2B%201)%20%20%2F%2F%20%5B0&#8230;7%5D%0Anew%20Random().nextInt(8%20%2B%201)%20%20%2F%2F%20%5B0&#8230;8%5D%0Anew%20Random().nextInt(9%20%2B%201)%20%20%2F%2F%20%5B0&#8230;9%5D%0Anew%20Random().nextInt(10%20%2B%201)%20%2F%2F%20%5B0&#8230;10%5D%0Anew%20Random().nextInt(11%20%2B%201)%20%2F%2F%20%5B0&#8230;11%5D%0A&#8221; message=&#8221;java code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\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[pastacode lang=&#8221;java&#8221; manual=&#8221;new%20Random().nextInt(5%20%2B%201)%20%20%2B%2010%20%2F%2F%20%5B0&#8230;5%5D%20%20%2B%2010%20%3D%20%5B10&#8230;15%5D%0Anew%20Random().nextInt(6%20%2B%201)%20%20%2B%2010%20%2F%2F%20%5B0&#8230;6%5D%20%20%2B%2010%20%3D%20%5B10&#8230;16%5D%0Anew%20Random().nextInt(7%20%2B%201)%20%20%2B%2010%20%2F%2F%20%5B0&#8230;7%5D%20%20%2B%2010%20%3D%20%5B10&#8230;17%5D%0Anew%20Random().nextInt(8%20%2B%201)%20%20%2B%2010%20%2F%2F%20%5B0&#8230;8%5D%20%20%2B%2010%20%3D%20%5B10&#8230;18%5D%0Anew%20Random().nextInt(9%20%2B%201)%20%20%2B%2010%20%2F%2F%20%5B0&#8230;9%5D%20%20%2B%2010%20%3D%20%5B10&#8230;19%5D%0Anew%20Random().nextInt(10%20%2B%201)%20%2B%2010%20%2F%2F%20%5B0&#8230;10%5D%20%2B%2010%20%3D%20%5B10&#8230;20%5D%0Anew%20Random().nextInt(11%20%2B%201)%20%2B%2010%20%2F%2F%20%5B0&#8230;11%5D%20%2B%2010%20%3D%20%5B10&#8230;21%5D%0A&#8221; message=&#8221;java code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\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[pastacode lang=&#8221;java&#8221; manual=&#8221;new%20Random().nextInt((max%20-%20min)%20%2B%201)%20%2B%20min%0Anew%20Random().nextInt((30%20-%2010)%20%2B%201)%20%2B%2010%0Anew%20Random().nextInt((20)%20%2B%201)%20%2B%2010%0Anew%20Random().nextInt(21)%20%2B%2010%20%20%20%20%2F%2F%5B0&#8230;20%5D%20%2B%2010%20%3D%20%5B10&#8230;30%5D%0A&#8221; message=&#8221;java code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\n<p><strong>\/\/5. Test [15&#8230;99]<\/strong><\/p>\n<p><strong>\/\/ min = 15 , max = 99, range = (max &#8211; min)<\/strong><\/p>\n[pastacode lang=&#8221;java&#8221; manual=&#8221;new%20Random().nextInt((max%20-%20min)%20%2B%201)%20%2B%20min%0Anew%20Random().nextInt((99%20-%2015)%20%2B%201)%20%2B%2015%0Anew%20Random().nextInt((84)%20%2B%201)%20%2B%2015%0Anew%20Random().nextInt(85)%20%2B%2015%20%20%20%20%2F%2F%5B0&#8230;84%5D%20%2B%2015%20%3D%20%5B15&#8230;99%5D%0A&#8221; message=&#8221;java code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\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[pastacode lang=&#8221;java&#8221; manual=&#8221;package%20com.wiki.example.test%3B%0Aimport%20java.util.Random%3B%0Apublic%20class%20WikitechyRandom%20%0A%7B%0A%20static%20void%20main(String%5B%5D%20args)%20%0A%7B%0Afor%20(int%20i%20%3D%200%3B%20i%20%3C%2010%3B%20i%2B%2B)%20%0A%7B%0ASystem.out.println(getRandomNumberInRange(5%2C%2010))%3B%0A%7D%0A%7D%0Aprivate%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%0A%7B%0Aif%20(min%20%3E%3D%20max)%20%0A%7B%0Athrow%20new%20IllegalArgumentException(%22max%20must%20be%20greater%20than%20min%22)%3B%0A%7D%0ARandom%20r%20%3D%20new%20Random()%3B%0Areturn%20r.nextInt((max%20-%20min)%20%2B%201)%20%2B%20min%3B%0A%7D%0A%7D%0Aprivate%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%20%0A%7B%0Aif%20(min%20%3E%3D%20max)%20%0A%7B%0Athrow%20new%20IllegalArgumentException(%22max%20must%20be%20greater%20than%20min%22)%3B%0A%7D%0ARandom%20r%20%3D%20new%20Random()%3B%0Areturn%20r.nextInt((max%20-%20min)%20%2B%201)%20%2B%20min%3B%0A%7D%0A%7D%0Aprivate%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%20%0A%7B%0Aif%20(min%20%3E%3D%20max)%20%0A%7B%0Athrow%20new%20IllegalArgumentException(%22max%20must%20be%20greater%20than%20min%22)%3B%0A%7D%0ARandom%20r%20%3D%20new%20Random()%3B%0Areturn%20r.nextInt((max%20-%20min)%20%2B%201)%20%2B%20min%3B%0A%7D%0A%7D%0Aprivate%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%20%7B%0Aif%20(min%20%3E%3D%20max)%20%0A%7B%0Athrow%20new%20IllegalArgumentException(%22max%20must%20be%20greater%20than%20min%22)%3B%0A%7D%0ARandom%20r%20%3D%20new%20Random()%3B%0Areturn%20r.nextInt((max%20-%20min)%20%2B%201)%20%2B%20min%3B%0A%7D%0A%7D%0A%0A%0A&#8221; message=&#8221;java code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\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[pastacode lang=&#8221;java&#8221; manual=&#8221;package%20com.wiki.example.test%3B%0Apublic%20class%20WikitechyRandom%20%0A%7B%0A%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%0A%7B%0A%0A%09%09for%20(int%20i%20%3D%200%3B%20i%20%3C%2010%3B%20i%2B%2B)%20%0A%7B%0A%09%09%09System.out.println(getRandomNumberInRange(26%2C%2030))%3B%0A%7D%0A%0A%7D%0Aprivate%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%20%0A%7B%0A%0A%09%09if%20(min%20%3E%3D%20max)%20%0A%7B%0A%09%09%09throw%20new%20IllegalArgumentException(%22max%20must%20be%20greater%20than%20min%22)%3B%0A%7D%0A%0A%09%09return%20(int)(Math.random()%20*%20((max%20-%20min)%20%2B%201))%20%2B%20min%3B%0A%7D%0A%0A%7D%0A&#8221; message=&#8221;java code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\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[pastacode lang=&#8221;java&#8221; manual=&#8221;private%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%20%0A%7B%0A%0A%09Random%20r%20%3D%20new%20Random()%3B%0A%09return%20r.ints(min%2C%20(max%20%2B%201)).findFirst().getAsInt()%3B%0A%0A%7D%0A&#8221; message=&#8221;java code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\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[pastacode lang=&#8221;java&#8221; manual=&#8221;package%20com.wiki.form.test%3B%0A%0Aimport%20java.util.Random%3B%0Apublic%20class%20WikitechyRandom%20%0A%7B%0Apublic%20static%20void%20main(String%5B%5D%20args)%20%0A%7B%0Afor%20(int%20i%20%3D%200%3B%20i%20%3C%2010%3B%20i%2B%2B)%20%0A%7B%0ASystem.out.println(getRandomNumberInRange(43%2C%2048))%3B%0A%7D%0A%7D%0Aprivate%20static%20int%20getRandomNumberInRange(int%20min%2C%20int%20max)%20%0A%7B%0ARandom%20r%20%3D%20new%20Random()%3B%0Areturn%20r.ints(min%2C%20(max%20%2B%201)).limit(1).findFirst().getAsInt()%3B%0A%7D%0A%7D%0A%0A%0A&#8221; message=&#8221;java code&#8221; highlight=&#8221;&#8221; provider=&#8221;manual&#8221;\/]\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}]}}