{"id":132,"date":"2020-03-03T06:25:00","date_gmt":"2020-03-03T06:25:00","guid":{"rendered":"https:\/\/molecularsciences.org\/content\/?p=132"},"modified":"2020-12-02T06:27:15","modified_gmt":"2020-12-02T06:27:15","slug":"generating-random-numbers-in-java","status":"publish","type":"post","link":"https:\/\/molecularsciences.org\/content\/generating-random-numbers-in-java\/","title":{"rendered":"Generating Random Numbers in Java"},"content":{"rendered":"\n<p>In programming, often we need to generate random numbers. All major languages provide functionality to generate pseudo-random numbers. In Java, this functionality is provided by java.util.Random. See example below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import java.util.Random;\n\npublic class RandomExample {\n    public static void main(String&#91;] args) {\n        Random rand = new Random();\n        rand.setSeed(12345);\n        System.out.println(rand.nextInt());\n        System.out.println(rand.nextDouble());\n        System.out.println(rand.nextInt(1111));\n        int x = rand.nextInt(501) + 500;\n        System.out.println(x);\n    }\n}<\/code><\/pre>\n\n\n\n<p><strong>output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1553932502\n0.5132095356731635\n241\n555<\/code><\/pre>\n\n\n\n<p>First import java.util.Random. Instantiate an object of the class Random. Pseudo-random numbers are initialized with a numerical seed. setSeed() sets this seed. nextInt() return an integer. nextDouble returns a Double. nextInt(1111) will return a random number between 0 and 1110. If I want a random number between 500 and 1000 inclusive, I need to run nextInt(501) which will give me number 0 &#8211; 500 inclusive. Then simply add 500 to the generated number to get a random number between 500 and 1000 inclusive.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In programming, often we need to generate random numbers. All major languages provide functionality to generate pseudo-random numbers. In Java, this functionality is provided by java.util.Random. See example below: output First import java.util.Random. Instantiate an object of the class Random. Pseudo-random numbers are initialized with a numerical seed. setSeed() sets this seed. nextInt() return an [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[38],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-java","tag-java"],"_links":{"self":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/132","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/comments?post=132"}],"version-history":[{"count":1,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/132\/revisions"}],"predecessor-version":[{"id":133,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/posts\/132\/revisions\/133"}],"wp:attachment":[{"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/categories?post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/molecularsciences.org\/content\/wp-json\/wp\/v2\/tags?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}