{"id":1571,"date":"2018-02-27T16:17:13","date_gmt":"2018-02-27T21:17:13","guid":{"rendered":"http:\/\/bluegalaxy.info\/codewalk\/?p=1571"},"modified":"2018-02-28T10:38:33","modified_gmt":"2018-02-28T15:38:33","slug":"javascript-generate-array-random-numbers","status":"publish","type":"post","link":"https:\/\/bluegalaxy.info\/codewalk\/2018\/02\/27\/javascript-generate-array-random-numbers\/","title":{"rendered":"JavaScript: How to generate an array of random numbers"},"content":{"rendered":"<p>Let&#8217;s say I want to create a list of 10 numbers that are between the values of 0 and 1. This can be done with a simple for loop. For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let length = 10;\r\nlet max = 1;\r\nlet randArray = [];\r\nfor (i=0; i &lt; length; i++) {\r\n    randArray.push(Math.random(max));\r\n}\r\nconsole.log(randArray);<\/pre>\n<p>Setting the max value to 1 gives me only random values between 0 and 1. Setting the length to 10 gives me a list of 10 numbers.\u00a0 This yields:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">[0.515654184126071, 0.3903385797823882, 0.9408518825018701, 0.08469998671516599, 0.2765966591556014, \r\n0.20919957282624446, 0.9498787982265717, 0.5195513802386978, 0.7100803314063749, 0.03271176762225736]<\/pre>\n<p>Now instead of values between 0 and 1, lets say I want values from 1 to 100 and I want no decimals:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">let length = 10;\r\nlet max = 100;\r\nlet randArray = [];\r\nfor (i=0; i &lt; length; i++) {\r\n randArray.push(Math.round(Math.random() * max));\r\n}\r\nconsole.log(randArray);<\/pre>\n<p>Here I wrapped the\u00a0<code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">Math.random()<\/code> function in a <code class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">Math.round()<\/code> function (so there are no decimals), and multiplied each number by the max. The result is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"no-highlight\">[75, 22, 66, 20, 24, 86, 66, 6, 59, 75]<\/pre>\n<p>For more information about Math.random(), see:<br \/>\n<a href=\"https:\/\/www.w3schools.com\/jsref\/jsref_random.asp\">https:\/\/www.w3schools.com\/jsref\/jsref_random.asp<\/a><br \/>\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Math\/random\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Math\/random<\/a><\/p>\n<p>For more information about Math.round(), see:<br \/>\n<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Math\/round\">https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Math\/round<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s say I want to create a list of 10 numbers that are between the values of 0 and 1. This can be done with a simple for loop. For example: let length = 10; let max = 1; let randArray = []; for (i=0; i &lt; length; i++) { randArray.push(Math.random(max)); } console.log(randArray); Setting the &hellip; <a href=\"https:\/\/bluegalaxy.info\/codewalk\/2018\/02\/27\/javascript-generate-array-random-numbers\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">JavaScript: How to generate an array of random numbers<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[44],"tags":[45,51,109],"class_list":["post-1571","post","type-post","status-publish","format-standard","hentry","category-javascript-language","tag-javascript","tag-math-random","tag-math-round"],"_links":{"self":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/comments?post=1571"}],"version-history":[{"count":8,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1571\/revisions"}],"predecessor-version":[{"id":1579,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/posts\/1571\/revisions\/1579"}],"wp:attachment":[{"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/media?parent=1571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/categories?post=1571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bluegalaxy.info\/codewalk\/wp-json\/wp\/v2\/tags?post=1571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}