Title: Math.random Java Example - Java Code Geeks
Open Graph Title: Math.random Java Example - Java Code Geeks
Description: In this article, we will explain Math.random method in Java through examples. Java provides us Math class, which includes methods with basic numeric
Open Graph Description: In this article, we will explain Math.random method in Java through examples. Java provides us Math class, which includes methods with basic numeric
Mail addresses
support@javacodegeeks.com
Opengraph URL: https://examples.javacodegeeks.com/math-random-java-example/
Generator: WordPress 6.9.4
Domain: examples.javacodegeeks.com
{"@context":"https://schema.org","@graph":[{"@type":"Article","@id":"https://examples.javacodegeeks.com/math-random-java-example/#article","isPartOf":{"@id":"https://examples.javacodegeeks.com/math-random-java-example/"},"author":{"name":"Katerina Zamani","@id":"https://examples.javacodegeeks.com/#/schema/person/94be714b6b9a82bb855c0d370c319673"},"headline":"Math.random Java Example","datePublished":"2014-03-07T18:07:37+00:00","dateModified":"2022-03-02T12:31:39+00:00","mainEntityOfPage":{"@id":"https://examples.javacodegeeks.com/math-random-java-example/"},"wordCount":374,"commentCount":0,"publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"image":{"@id":"https://examples.javacodegeeks.com/math-random-java-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","articleSection":["math"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https://examples.javacodegeeks.com/math-random-java-example/#respond"]}]},{"@type":"WebPage","@id":"https://examples.javacodegeeks.com/math-random-java-example/","url":"https://examples.javacodegeeks.com/math-random-java-example/","name":"Math.random Java Example - Java Code Geeks","isPartOf":{"@id":"https://examples.javacodegeeks.com/#website"},"primaryImageOfPage":{"@id":"https://examples.javacodegeeks.com/math-random-java-example/#primaryimage"},"image":{"@id":"https://examples.javacodegeeks.com/math-random-java-example/#primaryimage"},"thumbnailUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","datePublished":"2014-03-07T18:07:37+00:00","dateModified":"2022-03-02T12:31:39+00:00","description":"In this article, we will explain Math.random method in Java through examples. Java provides us Math class, which includes methods with basic numeric","breadcrumb":{"@id":"https://examples.javacodegeeks.com/math-random-java-example/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https://examples.javacodegeeks.com/math-random-java-example/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/math-random-java-example/#primaryimage","url":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg","width":150,"height":150,"caption":"Bipartite Graph"},{"@type":"BreadcrumbList","@id":"https://examples.javacodegeeks.com/math-random-java-example/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https://examples.javacodegeeks.com/"},{"@type":"ListItem","position":2,"name":"Java Development","item":"https://examples.javacodegeeks.com/category/java-development/"},{"@type":"ListItem","position":3,"name":"Core Java","item":"https://examples.javacodegeeks.com/category/java-development/core-java/"},{"@type":"ListItem","position":4,"name":"math","item":"https://examples.javacodegeeks.com/category/java-development/core-java/math/"},{"@type":"ListItem","position":5,"name":"Math.random Java Example"}]},{"@type":"WebSite","@id":"https://examples.javacodegeeks.com/#website","url":"https://examples.javacodegeeks.com/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https://examples.javacodegeeks.com/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https://examples.javacodegeeks.com/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https://examples.javacodegeeks.com/#organization","name":"Exelixis Media P.C.","url":"https://examples.javacodegeeks.com/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/logo/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2022/06/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https://examples.javacodegeeks.com/#/schema/logo/image/"},"sameAs":["https://www.facebook.com/javacodegeeks","https://x.com/javacodegeeks"]},{"@type":"Person","@id":"https://examples.javacodegeeks.com/#/schema/person/94be714b6b9a82bb855c0d370c319673","name":"Katerina Zamani","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https://examples.javacodegeeks.com/#/schema/person/image/","url":"https://examples.javacodegeeks.com/wp-content/uploads/2013/11/Katerina-Zamani-96x96.jpg","contentUrl":"https://examples.javacodegeeks.com/wp-content/uploads/2013/11/Katerina-Zamani-96x96.jpg","caption":"Katerina Zamani"},"description":"Katerina has graduated from the Department of Informatics and Telecommunications in National and Kapodistrian University of Athens (NKUA) and she attends MSc courses in Advanced Information Systems at the same department. Currently, her main academic interests focus on web applications, mobile development, software engineering, databases and telecommunications.","sameAs":["https://www.javacodegeeks.com"],"url":"https://examples.javacodegeeks.com/author/katerina-zamani/"}]}
{"@context":"http:\/\/schema.org","@type":"BreadcrumbList","@id":"#Breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"item":{"name":"Home","@id":"https:\/\/examples.javacodegeeks.com\/"}},{"@type":"ListItem","position":2,"item":{"name":"Java Development","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/"}},{"@type":"ListItem","position":3,"item":{"name":"Core Java","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/"}},{"@type":"ListItem","position":4,"item":{"name":"math","@id":"https:\/\/examples.javacodegeeks.com\/category\/java-development\/core-java\/math\/"}}]}
{"@context":"http:\/\/schema.org","@type":"Article","dateCreated":"2014-03-07T20:07:37+02:00","datePublished":"2014-03-07T20:07:37+02:00","dateModified":"2022-03-02T14:31:39+02:00","headline":"Math.random Java Example","name":"Math.random Java Example","keywords":[],"url":"https:\/\/examples.javacodegeeks.com\/math-random-java-example\/","description":"In this article, we will explain Math.random method in Java through examples. Java provides us Math class, which includes methods with basic numeric operations such as logarithm, square root, etc. One","copyrightYear":"2014","articleSection":"math","articleBody":"\nIn this article, we will explain Math.random method in Java through examples. \n\n\n\nJava provides us Math class, which includes methods with basic numeric operations such as logarithm, square root, etc. One of these methods is random(), which gives us a pseudorandom positive double number greater than or equal to 0.0 and less than 1.0 - [0.0, 1.0).\n\n\n\nYou can learn more through our Java Math Operators and Math Class Tutorial.\n\n\n\n\n\n\n\nIn this example, we are going to show how to produce integer and double random numbers between a defined space, via math.random method in Java.\n\n\n\n1. Example of Math.random method in Java\n\n\n\nCreate a java class with name MathRandom Class and paste the following code.\n\n\n\nMathRandomClass.java\n\n\n\n\n\n\n\n\n\n01\n02\n03\n04\n05\n06\n07\n08\n09\n10\n11\n12\n13\n14\n15\n16\n17\n18\n19\n20\n21\n22\n23\n24\n25\n26\n\n\n\npackage com.javacodegeeks.javacore.mathrandom;\n \npublic class MathRandomClass {\n \n public static void main(String[] args) {\n \/\/ TODO Auto-generated method stub\n \n \/\/ a simple random number\n double x = Math.random();\n System.out.println(\"Double between 0.0 and 1.0: x = \"+x);\n \n \/\/ double between [0.0, 20.0)\n double y = Math.random()*20.0;\n System.out.println(\"Double between 0.0 and 20.0: y = \"+y);\n \n \/\/ integer between [3,7]\n int r1 = (int) (Math.random()*5)+3;\n System.out.println(\"Integer between 3 and 8: r1 = \"+r1);\n \n \/\/ integer between [-10,10) - maximum 9\n int r2 = (int) (Math.random()*20)-10;\n System.out.println(\"Integer between -10 and 10: r2 = \"+r2);\n \n }\n \n}\n\n\n\n\n\n\n\n\n\n\nNow lets explain the code above. \n\n\n\nFirstly, we use Java math.random method in order to take a positive signed double value, that belongs to the range [0.0, 0.1). For generating random integer or double numbers between a range, we should multiply and\/or sum the appropriate positive and\/or negative values in order to achieve the desired result. For example, if we want a random positive, double number greater than or equal to 0.0 but less than 20.0, we should multiply the result of random() method with the double number 20.0. Respectively, if we multiply the result with 5 and add number 3, the range has a minimum value number 3 and as maximum value their sum (5+3). Please notice that the random values are always less than this sum \u2013 in our example the range is [3,8). Also notice that for integer values we should cast the result. We can use the same way for negative random numbers etc.\n\n\n\nFor a better understanding, please look the output of the execution. As you can notice, all the parameters take a value that is into their respective range.\n\n\n\nOutput\n\n\n\nDouble between 0.0 and 1.0: x = 0.9749288905833321\nDouble between 0.0 and 20.0: y = 14.067387697238761\nInteger between 3 and 8: r1 = 7\nInteger between -10 and 10: r2 = -9\n\n\n\nYou can learn more about the math.random method in Java in our Java random number generator Example.\n\n\n\n2. More articles\n\n\n\nJava random number generator ExampleWhat is Java used forJava Tutorial for BeginnersBest Way to Learn Java Programming Online\n\n\n\n3. Download the Source Code\n\n\n\nThis was a tutorial about Math.random method in Java. \n\n\n\nDownload\nDownload the source code of this example: Math.random Java Example\n\n\n\nLast updated on March 2nd, 2022\n","publisher":{"@id":"#Publisher","@type":"Organization","name":"Examples Java Code Geeks","logo":{"@type":"ImageObject","url":"\/wp-content\/uploads\/2013\/05\/JavaCodeGeeks-logo.png"},"sameAs":["https:\/\/feeds.feedburner.com\/examplesjavacodegeeks","https:\/\/www.facebook.com\/javacodegeeks","https:\/\/twitter.com\/javacodegeeks","https:\/\/www.linkedin.com\/groups\/3810709\/","https:\/\/www.youtube.com\/channel\/UCxoUc7Rar2q90Gu0nT2ffuQ","https:\/\/github.com\/javacodegeeks\/"]},"sourceOrganization":{"@id":"#Publisher"},"copyrightHolder":{"@id":"#Publisher"},"mainEntityOfPage":{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/math-random-java-example\/","breadcrumb":{"@id":"#Breadcrumb"}},"author":{"@type":"Person","name":"Katerina Zamani","url":"https:\/\/examples.javacodegeeks.com\/author\/katerina-zamani\/"},"image":{"@type":"ImageObject","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/java-logo.jpg","width":1200,"height":150}}
| og:locale | en_US |
| og:type | article |
| og:site_name | Examples Java Code Geeks |
| article:publisher | https://www.facebook.com/javacodegeeks |
| article:published_time | 2014-03-07T18:07:37+00:00 |
| article:modified_time | 2022-03-02T12:31:39+00:00 |
| og:image | https://examples.javacodegeeks.com/wp-content/uploads/2012/12/java-logo.jpg |
| og:image:width | 150 |
| og:image:height | 150 |
| og:image:type | image/jpeg |
| author | Katerina Zamani |
| twitter:card | summary_large_image |
| twitter:creator | @javacodegeeks |
| twitter:label1 | Written by |
| twitter:data1 | Katerina Zamani |
| twitter:label2 | Est. reading time |
| twitter:data2 | 3 minutes |
| None | IE=edge |
| msapplication-TileImage | https://examples.javacodegeeks.com/wp-content/uploads/2013/05/JavaCodeGeeks-favIcon.png |
Links:
Viewport: width=device-width, initial-scale=1.0
Robots: index, follow, max-image-preview:large, max-snippet:-1, max-video-preview:-1