{"id":2268,"date":"2018-06-06T08:11:34","date_gmt":"2018-06-06T08:11:34","guid":{"rendered":"https:\/\/www.testingdocs.com\/questions\/?p=2268"},"modified":"2021-09-28T12:03:21","modified_gmt":"2021-09-28T12:03:21","slug":"how-to-create-an-array-with-random-values-in-a-java-program","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/how-to-create-an-array-with-random-values-in-a-java-program\/","title":{"rendered":"Create an array with random values in a java program"},"content":{"rendered":"<p>In this post, we will create an array and populate the array with some random values.<\/p>\n<h3>Declare an array<\/h3>\n<p>We can declare an array of integers using the syntax:<\/p>\n<p>int[] array;<\/p>\n<h3>Create an array with random values<\/h3>\n<p>We can create an array of ten integers using the syntax.<\/p>\n<p>array = new int[10];<\/p>\n<h3>Java program<\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">import java.util.Random;\r\n\r\npublic class RandomArrayDemo {\r\n  public static void main(String[] args) {\r\n    int[] array;\t        \/\/ declare an array\r\n    array = new int[10];\t\/\/ create an array of ten integers\r\n    Random rand =new Random();\r\n    for (int i = 0; i &lt; array.length; i++) {\r\n      array[i] = rand.nextInt(100); \/\/ generate random number\r\n      System.out.print(array[i] + \" \");\r\n    }\r\n  }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h3><\/h3>\n<h3>Sample Output<\/h3>\n<p>40 26 32 73 5 8 15 16 86 25<\/p>\n<p>&nbsp;<\/p>\n<p>Note that : As expected for each run of the program the values in the array would change.<\/p>\n<h3>Screenshot<\/h3>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2269\" src=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Array-with-Random-values.png\" alt=\"Array with Random values\" width=\"1738\" height=\"1009\" title=\"\" srcset=\"https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Array-with-Random-values.png 1738w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Array-with-Random-values-300x174.png 300w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Array-with-Random-values-1024x594.png 1024w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Array-with-Random-values-768x446.png 768w, https:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Array-with-Random-values-1536x892.png 1536w\" sizes=\"auto, (max-width: 1738px) 100vw, 1738px\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&#8212;<\/p>\n<p>Java Tutorial on this website:<\/p>\n<p><a href=\"https:\/\/www.testingdocs.com\/java-tutorial\/\">https:\/\/www.testingdocs.com\/java-tutorial\/<\/a><\/p>\n<p>For more information on Java, visit the official website :<\/p>\n<p><a href=\"https:\/\/www.oracle.com\/in\/java\/\" rel=\"noopener\">https:\/\/www.oracle.com\/in\/java\/<\/a><\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-2268\" data-postid=\"2268\" class=\"themify_builder_content themify_builder_content-2268 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>In this post, we will create an array and populate the array with some random values. Declare an array We can declare an array of integers using the syntax: int[] array; Create an array with random values We can create an array of ten integers using the syntax. array = new int[10]; Java program import [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-2268","post","type-post","status-publish","format-standard","hentry","category-java-programs","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2268","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=2268"}],"version-history":[{"count":6,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2268\/revisions"}],"predecessor-version":[{"id":21216,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/2268\/revisions\/21216"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=2268"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=2268"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=2268"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}