{"id":3069,"date":"2021-10-01T14:07:58","date_gmt":"2021-10-01T14:07:58","guid":{"rendered":"https:\/\/rengga.dev\/blog\/?p=3069"},"modified":"2023-02-28T00:48:21","modified_gmt":"2023-02-28T00:48:21","slug":"js-tutorial-math-random-api-key-generator","status":"publish","type":"post","link":"https:\/\/rengga.dev\/blog\/js-tutorial-math-random-api-key-generator\/","title":{"rendered":"JS Tutorial &#8211; Math.random() API Key Generator"},"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><iframe style=\"width: 100%;\" title=\"Untitled\" src=\"https:\/\/codepen.io\/renggagumilar\/embed\/abYREvL?default-tab=result&amp;theme-id=dark\" height=\"300\" frameborder=\"no\" scrolling=\"no\" allowfullscreen=\"allowfullscreen\"><br \/>\nSee the Pen <a href=\"https:\/\/codepen.io\/renggagumilar\/pen\/abYREvL\"><br \/>\nUntitled<\/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<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\/**\r\n * Function to produce UUID.\r\n * See: http:\/\/stackoverflow.com\/a\/8809472\r\n *\/\r\nfunction generateUUID()\r\n{\r\n    var d = new Date().getTime();\r\n    \r\n    if( window.performance &amp;&amp; typeof window.performance.now === \"function\" )\r\n    {\r\n        d += performance.now();\r\n    }\r\n    \r\n    var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(\/[xy]\/g, function(c)\r\n    {\r\n        var r = (d + Math.random()*16)%16 | 0;\r\n        d = Math.floor(d\/16);\r\n        return (c=='x' ? r : (r&amp;0x3|0x8)).toString(16);\r\n    });\r\n\r\nreturn uuid;\r\n}\r\n\r\n\/**\r\n * Generate new key and insert into input value\r\n *\/\r\n$( '#keygen' ).on('click',function()\r\n{\r\n    $( '#apikey' ).val( generateUUID() );\r\n});<\/pre>\n<p>Here\u2019s a super real-world practical use case for random numbers! The demo generates 16 random numbers to create a universally unique identifier (UUID) that can be used as a key that provides access to an API.<\/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-api-key-generator\/\" title=\"JS Tutorial &#8211; Math.random() API Key Generator\" itemprop=\"url\"><\/a><\/p>\n","protected":false},"author":1,"featured_media":3801,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[281,264,263,274,103,227],"newstopic":[],"class_list":{"0":"post-3069","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-javascript","8":"tag-api-key-generator","9":"tag-javascript-tutorial","10":"tag-js-tutorial","11":"tag-math-random","12":"tag-web-design","13":"tag-web-designer"},"_links":{"self":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3069","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=3069"}],"version-history":[{"count":1,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3069\/revisions"}],"predecessor-version":[{"id":3071,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/posts\/3069\/revisions\/3071"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media\/3801"}],"wp:attachment":[{"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/media?parent=3069"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/categories?post=3069"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/tags?post=3069"},{"taxonomy":"newstopic","embeddable":true,"href":"https:\/\/rengga.dev\/blog\/wp-json\/wp\/v2\/newstopic?post=3069"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}