{"id":3059,"date":"2021-10-29T13:30:54","date_gmt":"2021-10-29T13:30:54","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=3059"},"modified":"2023-06-12T14:43:46","modified_gmt":"2023-06-12T14:43:46","slug":"js-tutorial-math-random-random-background-color","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/js-tutorial-math-random-random-background-color\/","title":{"rendered":"JS Tutorial &#8211; Math.random() Random Background Color"},"content":{"rendered":"<p><span style=\"color: #ef3207;\"><a style=\"color: #ef3207;\" href=\"https:\/\/rengga.dev\/\" target=\"_blank\" rel=\"noopener\"><strong>Rengga Dev<\/strong><\/a> <\/span>&#8211; <code>Math.random()<\/code> is an API in JavaScript. It is a function that gives you a random number. The number returned will be between 0 (inclusive, as in, it\u2019s possible for an actual 0 to be returned) and 1 (exclusive, as in, it\u2019s not possible for an actual 1 to be returned).<\/p>\n<p>This is where the magic happens:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">const random = (min, max) =&gt; {\r\n  return Math.floor(Math.random() * (max - min + 1)) + min;\r\n}<\/pre>\n<p>The first line of code randomly shuffles the array and the second line returns a random umber between 0 and 10. In the example of a random color background, the range of colors and specifics such as hues, saturations, and shades can be set.<\/p>\n<p>For another method for generating a random hex color, check out\u00a0this article\u00a0by Chris Coyer.<br \/>\n<iframe style=\"width: 100%;\" title=\"JS Tutorial - Generative Color Harmonies\" src=\"https:\/\/codepen.io\/renggagumilar\/embed\/wvmYPEN?default-tab=result&amp;theme-id=dark\" height=\"600\" frameborder=\"no\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><br \/>\nSee the Pen <a href=\"https:\/\/codepen.io\/renggagumilar\/pen\/wvmYPEN\"><br \/>\nJS Tutorial &#8211; Generative Color Harmonies<\/a> by Rengga Gumilar (<a href=\"https:\/\/codepen.io\/renggagumilar\">@renggagumilar<\/a>)<br \/>\non <a href=\"https:\/\/codepen.io\">CodePen<\/a>.<br \/>\n<\/iframe><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rengga Dev &#8211; Math.random() is an API in JavaScript. It is a <a class=\"read-more\" href=\"https:\/\/rengga.dev\/blog\/js-tutorial-math-random-random-background-color\/\" title=\"JS Tutorial &#8211; Math.random() Random Background Color\" itemprop=\"url\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":3793,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,12],"tags":[264,263,274,278,103,227],"newstopic":[571],"class_list":{"0":"post-3059","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript","8":"category-web-development","9":"tag-javascript-tutorial","10":"tag-js-tutorial","11":"tag-math-random","12":"tag-random-background-color","13":"tag-web-design","14":"tag-web-designer","15":"newstopic-javascript"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3059","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/comments?post=3059"}],"version-history":[{"count":1,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3059\/revisions"}],"predecessor-version":[{"id":3061,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3059\/revisions\/3061"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media\/3793"}],"wp:attachment":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media?parent=3059"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=3059"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=3059"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=3059"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}