{"id":1534,"date":"2022-04-05T16:13:16","date_gmt":"2022-04-05T04:13:16","guid":{"rendered":"https:\/\/www.ronella.xyz\/?p=1534"},"modified":"2022-04-05T16:13:16","modified_gmt":"2022-04-05T04:13:16","slug":"generating-random-number","status":"publish","type":"post","link":"https:\/\/www.ronella.xyz\/?p=1534","title":{"rendered":"Generating Random Number"},"content":{"rendered":"<h2>Listing the Available Random Algorithms<\/h2>\n<pre><code class=\"language-java\">System.out.printf(&quot;%-13s| %s\\n-------------|---------------------\\n&quot;, &quot;Group&quot;, &quot;Name&quot;);\njava.util.random.RandomGeneratorFactory.all()\n        .sorted(java.util.Comparator.comparing(java.util.random.RandomGeneratorFactory::name))\n        .forEach(factory -&gt; System.out.printf(&quot;%-13s| %s\\n&quot;, factory.group(), factory.name()));<\/code><\/pre>\n<p>The output is similar to the following:<\/p>\n<pre><code>Group        | Name\n-------------|---------------------\nLXM          | L128X1024MixRandom\nLXM          | L128X128MixRandom\nLXM          | L128X256MixRandom\nLXM          | L32X64MixRandom\nLXM          | L64X1024MixRandom\nLXM          | L64X128MixRandom\nLXM          | L64X128StarStarRandom\nLXM          | L64X256MixRandom\nLegacy       | Random\nLegacy       | SecureRandom\nLegacy       | SplittableRandom\nXoroshiro    | Xoroshiro128PlusPlus\nXoshiro      | Xoshiro256PlusPlus<\/code><\/pre>\n<blockquote>\n<p>Check from the following address on how to choose which algorithm to choose:<\/p>\n<p><a href=\"https:\/\/docs.oracle.com\/en\/java\/javase\/17\/docs\/api\/java.base\/java\/util\/random\/package-summary.html#algorithms\">https:\/\/docs.oracle.com\/en\/java\/javase\/17\/docs\/api\/java.base\/java\/util\/random\/package-summary.html#algorithms<\/a><\/p>\n<\/blockquote>\n<h3>Creating an instance of RandomGenerator<\/h3>\n<h4>The default algorithm <em>(i.e. L32X64MixRandom)<\/em><\/h4>\n<pre><code class=\"language-java\">var generator = java.util.random.RandomGenerator.getDefault();<\/code><\/pre>\n<h4>The specific algorithm (e.g. L64X128MixRandom)<\/h4>\n<pre><code class=\"language-java\">var generator = java.util.random.RandomGenerator.of(&quot;L64X128MixRandom&quot;);<\/code><\/pre>\n<blockquote>\n<p>Use the <strong>static method RandomGenerator.of<\/strong> and pass the <strong>name of the algorithm<\/strong>.<\/p>\n<\/blockquote>\n<h2>Generating Random Number<\/h2>\n<p>Use one of the following <strong>instance methods of the RandomGenerator class<\/strong>:<\/p>\n<ul>\n<li>nextDouble()<\/li>\n<li>nextDouble(double bound)<\/li>\n<li>nextDouble(double origin, double bound)<\/li>\n<li>nextFloat()<\/li>\n<li>nextFloat(float bound)<\/li>\n<li>nextFloat(float origin, float bound)<\/li>\n<li>nextInt()<\/li>\n<li>nextInt(int bound)<\/li>\n<li>nextInt(int origin, int bound)<\/li>\n<li>nextLong()<\/li>\n<li>nextLong(long bound)<\/li>\n<li>nextLong(long origin, long bound)<\/li>\n<\/ul>\n<p>The <strong>origin parameter is inclusive<\/strong> and <strong>bound parameter is exclusive<\/strong>.<\/p>\n<p><strong>Example using a classic random algorithm<\/strong><\/p>\n<pre><code class=\"language-java\"> var generator = java.util.random.RandomGenerator.of(&quot;Random&quot;);\n System.out.println(generator.nextInt());<\/code><\/pre>\n<blockquote>\n<p>The output is a random integer number.<\/p>\n<\/blockquote>\n<h2>Generating Random Number Using Streams<\/h2>\n<p>Use one of the following <strong>instance methods of RandomGenerators class to create streams<\/strong>:<\/p>\n<ul>\n<li>doubles()<\/li>\n<li>doubles(double origin, double bound)<\/li>\n<li>doubles(long streamSize)<\/li>\n<li>doubles(long streamSize, double origin, double bound)<\/li>\n<li>ints()<\/li>\n<li>ints(int origin, int bound)<\/li>\n<li>ints(long streamSize)<\/li>\n<li>ints(long streamSize, int origin, int bound)<\/li>\n<li>longs()<\/li>\n<li>longs(long origin, long bound)<\/li>\n<li>longs(long streamSize)<\/li>\n<li>longs(long streamSize, long origin, long bound)<\/li>\n<\/ul>\n<p>The <strong>origin parameter is inclusive<\/strong> and <strong>bound parameter is exclusive<\/strong>.<\/p>\n<p><strong>Example using a classic random algorithm<\/strong><\/p>\n<pre><code class=\"language-java\">var generator = java.util.random.RandomGenerator.of(&quot;Random&quot;);\nvar randomInt = generator.ints(100, 1, 100)\n        .filter(number -&gt; number % 2 == 0 )\n        .findFirst();\nrandomInt.ifPresent(System.out::println);<\/code><\/pre>\n<blockquote>\n<p>The output is a random integer number.<\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>Listing the Available Random Algorithms System.out.printf(&quot;%-13s| %s\\n&#8212;&#8212;&#8212;&#8212;-|&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;\\n&quot;, &quot;Group&quot;, &quot;Name&quot;); java.util.random.RandomGeneratorFactory.all() .sorted(java.util.Comparator.comparing(java.util.random.RandomGeneratorFactory::name)) .forEach(factory -&gt; System.out.printf(&quot;%-13s| %s\\n&quot;, factory.group(), factory.name())); The output is similar to the following: Group | Name &#8212;&#8212;&#8212;&#8212;-|&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; LXM | L128X1024MixRandom LXM | L128X128MixRandom LXM | L128X256MixRandom LXM | L32X64MixRandom LXM | L64X1024MixRandom LXM | L64X128MixRandom LXM | L64X128StarStarRandom LXM | L64X256MixRandom Legacy | Random [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[74],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1534"}],"collection":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1534"}],"version-history":[{"count":1,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1534\/revisions"}],"predecessor-version":[{"id":1535,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=\/wp\/v2\/posts\/1534\/revisions\/1535"}],"wp:attachment":[{"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1534"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1534"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ronella.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1534"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}