{"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=\u201djava\u201d manual=\u201dprivate%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\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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 \u2013 min)<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201d%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\u20264%5D%20%5Bmin%20%3D%200%2C%20max%20%3D%204%5D%0Anew%20Random().nextInt(6)%3B%20%20%2F%2F%20%5B0\u20265%5D%0Anew%20Random().nextInt(7)%3B%20%20%2F%2F%20%5B0\u20266%5D%0Anew%20Random().nextInt(8)%3B%20%20%2F%2F%20%5B0\u20267%5D%0Anew%20Random().nextInt(9)%3B%20%20%2F%2F%20%5B0\u20268%5D%0Anew%20Random().nextInt(10)%3B%20%2F%2F%20%5B0\u20269%5D%0Anew%20Random().nextInt(11)%3B%20%2F%2F%20%5B0\u202610%5D%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\n<p><strong>\/\/2. To include the last value (max value) = (range + 1)<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201dnew%20Random().nextInt(5%20%2B%201)%20%20%2F%2F%20%5B0\u20265%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\u20266%5D%0Anew%20Random().nextInt(7%20%2B%201)%20%20%2F%2F%20%5B0\u20267%5D%0Anew%20Random().nextInt(8%20%2B%201)%20%20%2F%2F%20%5B0\u20268%5D%0Anew%20Random().nextInt(9%20%2B%201)%20%20%2F%2F%20%5B0\u20269%5D%0Anew%20Random().nextInt(10%20%2B%201)%20%2F%2F%20%5B0\u202610%5D%0Anew%20Random().nextInt(11%20%2B%201)%20%2F%2F%20%5B0\u202611%5D%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201djava\u201d manual=\u201dnew%20Random().nextInt(5%20%2B%201)%20%20%2B%2010%20%2F%2F%20%5B0\u20265%5D%20%20%2B%2010%20%3D%20%5B10\u202615%5D%0Anew%20Random().nextInt(6%20%2B%201)%20%20%2B%2010%20%2F%2F%20%5B0\u20266%5D%20%20%2B%2010%20%3D%20%5B10\u202616%5D%0Anew%20Random().nextInt(7%20%2B%201)%20%20%2B%2010%20%2F%2F%20%5B0\u20267%5D%20%20%2B%2010%20%3D%20%5B10\u202617%5D%0Anew%20Random().nextInt(8%20%2B%201)%20%20%2B%2010%20%2F%2F%20%5B0\u20268%5D%20%20%2B%2010%20%3D%20%5B10\u202618%5D%0Anew%20Random().nextInt(9%20%2B%201)%20%20%2B%2010%20%2F%2F%20%5B0\u20269%5D%20%20%2B%2010%20%3D%20%5B10\u202619%5D%0Anew%20Random().nextInt(10%20%2B%201)%20%2B%2010%20%2F%2F%20%5B0\u202610%5D%20%2B%2010%20%3D%20%5B10\u202620%5D%0Anew%20Random().nextInt(11%20%2B%201)%20%2B%2010%20%2F%2F%20%5B0\u202611%5D%20%2B%2010%20%3D%20%5B10\u202621%5D%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>\/\/ Range = (max \u2013 min)<\/strong><\/p>\n<p><strong>\/\/ So, the final formula is ((max \u2013 min) + 1) + min<\/strong><\/p>\n<p>\u00a0<\/p>\n<p><strong>\/\/4. Test [10\u202630]<\/strong><\/p>\n<p><strong>\/\/ min = 10 , max = 30, range = (max \u2013 min)<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201dnew%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\u202620%5D%20%2B%2010%20%3D%20%5B10\u202630%5D%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n<p><strong>\/\/5. Test [15\u202699]<\/strong><\/p>\n<p><strong>\/\/ min = 15 , max = 99, range = (max \u2013 min)<\/strong><\/p>\n[pastacode lang=\u201djava\u201d manual=\u201dnew%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\u202684%5D%20%2B%2015%20%3D%20%5B15\u202699%5D%0A\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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=\u201djava\u201d manual=\u201dpackage%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\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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 \u2013 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=\u201djava\u201d manual=\u201dpackage%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\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\n[ad type=\u201dbanner\u201d]\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=\u201djava\u201d manual=\u201dprivate%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\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201djava\u201d manual=\u201dpackage%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\u201d message=\u201djava code\u201d highlight=\u201d\u201d provider=\u201dmanual\u201d\/]\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=\u201dbanner\u201d]\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}]}}