{"id":465,"date":"2011-03-26T04:32:51","date_gmt":"2011-03-26T10:32:51","guid":{"rendered":"http:\/\/bateru.com\/news\/?p=465"},"modified":"2011-08-30T00:10:50","modified_gmt":"2011-08-30T06:10:50","slug":"code-of-the-day-javascript-random-number-generator","status":"publish","type":"post","link":"https:\/\/bateru.com\/news\/2011\/03\/code-of-the-day-javascript-random-number-generator\/","title":{"rendered":"Code of the Day: Javascript Random Number Generator"},"content":{"rendered":"<p><b>Code<\/b><br \/>\n<b>*Updated*<\/b><\/p>\n<pre lang='javascript' line='1'>\r\n\/\/@function: getRandomNum( i, decLen )\r\n\/**\r\n* @param i, a number for the number range. [0, i)\r\n* @param decLen, the number of fixed decimal length.\r\n* @returns number that is either a float( number with decimals ) or integer (whole number).\r\n* @note if not arguments are passed, then 0 or 1 is returned.\r\n*\/\r\nvar getRandomNum = function( i, decLen ){\r\n\treturn (Math.random() * (i||1) ).toFixed(decLen);\r\n};\r\n<\/pre>\n<p><b>Demo<\/b><br \/>\n<a href=\"http:\/\/jsbin.com\/uvuze3\/2\/\">http:\/\/jsbin.com\/uvuze3\/2\/<\/a><\/p>\n<p><b>Parameters<\/b><br \/>\nTodays code of the day is a random number generator coded in javascript.<br \/>\ngetRandomNum() enhances the Math.random() function that javascript provides by offering you the ability to get a random float ( number with decimals ) or integer( whole number ) value.<\/p>\n<p><b>Example output<\/b><\/p>\n<pre lang='javascript' line='1'>\r\n\/\/get value from Math.random()\r\ngetRandomNum();\t\/\/might return 0\r\n\/\/get value between [0,1) with 5 decimals\r\ngetRandomNum(1,5);\t\/\/might return 0.83034\r\n\/\/get value from [0,99]\r\ngetRandomNum(100);\t\/\/might return 84\r\n<\/pre>\n<p>\n<iframe src=\"http:\/\/rcm.amazon.com\/e\/cm?t=baterucom-20&#038;o=1&#038;p=8&#038;l=as1&#038;asins=B004S8L9LY&#038;ref=tf_til&#038;fc1=000000&#038;IS2=1&#038;lt1=_blank&#038;m=amazon&#038;lc1=0000FF&#038;bc1=000000&#038;bg1=FFFFFF&#038;f=ifr\" style=\"width:120px;height:240px;\" scrolling=\"no\" marginwidth=\"0\" marginheight=\"0\" frameborder=\"0\"><\/iframe><\/p>\n<p>Please leave a comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Code *Updated* \/\/@function: getRandomNum( i, decLen ) \/** * @param i, a number for the number range. [0, i) * @param decLen, the number of fixed decimal length. * @returns number that is either a float( number with decimals ) or integer (whole number). * @note if not arguments are passed, then 0 or 1 &hellip; <a href=\"https:\/\/bateru.com\/news\/2011\/03\/code-of-the-day-javascript-random-number-generator\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Code of the Day: Javascript Random Number Generator<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,10],"tags":[],"class_list":["post-465","post","type-post","status-publish","format-standard","hentry","category-frontend-tech","category-tutorials"],"_links":{"self":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/465","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/comments?post=465"}],"version-history":[{"count":10,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/465\/revisions"}],"predecessor-version":[{"id":733,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/posts\/465\/revisions\/733"}],"wp:attachment":[{"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/media?parent=465"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/categories?post=465"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bateru.com\/news\/wp-json\/wp\/v2\/tags?post=465"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}