Warning: The magic method EDD_Blocks::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php on line 101

Warning: The magic method EDD_Blocks::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks/edd-blocks.php on line 101

Warning: The magic method GAINWP_Manager::__wakeup() must have public visibility in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/gainwp.php on line 78

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 555

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 585

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 617

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 651

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 686

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 745

Deprecated: Optional parameter $filter declared before required parameter $metric is implicitly treated as a required parameter in /home/wcc1969/public_html/phasergames.com/wp-content/plugins/ga-in/tools/gapi.php on line 785

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home/wcc1969/public_html/phasergames.com/wp-content/plugins/edd-blocks-master/edd-blocks.php:101) in /home/wcc1969/public_html/phasergames.com/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":54,"date":"2017-05-17T21:45:46","date_gmt":"2017-05-17T21:45:46","guid":{"rendered":"http:\/\/phasergames.com\/uncategorized\/generate-a-random-string-in-javascriptphaser\/"},"modified":"2017-05-17T22:16:51","modified_gmt":"2017-05-17T22:16:51","slug":"generate-a-random-string-in-javascriptphaser","status":"publish","type":"post","link":"https:\/\/phasergames.com\/generate-a-random-string-in-javascriptphaser\/","title":{"rendered":"Generate a random string in Javascript\/Phaser"},"content":{"rendered":"

One of the thing I\u2019ve had to do commonly over the years in making games is to be able to generate a random string. Now lot of times I do this on the server side of code to be able to generate a none, one time use code. But there are times where I have had to do it on the client side maybe to generate a password would be a common use of this, or it could be having to guess what the random letters are in a mastermind type of game. It’s a very simple thing to do.
\nAll we need to do is to define a set of letters or symbols characters for the program to pick from. We decide on a word length and then loop through that many times pick a random index from the length of the set and then concat a string with that.<\/p>\n

Here’s the outline of the code.<\/p>\n

    \n
  1. make a random string of letters
    \nvar letters = “ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789*#%&!”;<\/li>\n
  2. we set up a variable called word set to blank
    \nvar word = “”;<\/li>\n
  3. Pick a random place in the string
    \nvar index = Math.floor(Math.random() * letters.length);<\/li>\n
  4. Concat the character at the place in the letters string to a string called word
    \nword += letters.charAt(index);<\/li>\n
  5. Do that for the number of times that we want the word length to be
    \nvar wordLen = 10;
    \nfor (var i = 0; i < wordLen; i++) {}<\/li>\n
  6. Return that value
    \nReturn word<\/li>\n<\/ol>\n

    Here is the full code<\/p>\n

    var StateMain = {\r\n    preload: function() {\r\n        game.load.image(\"button\", \"images\/btnGenerate.png\");\r\n    },\r\n    create: function() {\r\n        console.log(\"Ready!\");\r\n        var btnGet = game.add.sprite(game.world.centerX, game.world.centerY, \"button\");\r\n        this.text1 = game.add.text(game.world.centerX, game.height \/ 3, \"\");\r\n        this.text1.fill = \"#ffffff\";\r\n        btnGet.anchor.set(0.5, 0.5);\r\n        this.text1.anchor.set(0.5, 0.5);\r\n        btnGet.inputEnabled = true;\r\n        btnGet.events.onInputDown.add(this.getWord, this);\r\n    },\r\n    getWord: function() {\r\n        var word = this.makeRandomString();\r\n        this.text1.text = word;\r\n    },\r\n    makeRandomString: function() {\r\n        var letters = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789*#%&!\";\r\n        var word = \"\";\r\n       \r\n        var wordLen = 10;\r\n        for (var i = 0; i < wordLen; i++) {\r\n            var index = Math.floor(Math.random() * letters.length);\r\n            word += letters.charAt(index);\r\n        }\r\n        return word;\r\n    },\r\n    update: function() {}\r\n}<\/pre>\n

     <\/p>\n

    And Here is the result<\/p>\n

    \n<\/p>\n<\/div>\n

    Source Code Available in the FILE VAULT!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"

    One of the thing I\u2019ve had to do commonly over the years in making games is to be able to generate a random string. Now lot of times I do this on the server side of code to be able to generate a none, one time use code. But there are times where I have […]<\/p>\n","protected":false},"author":1,"featured_media":86,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[30],"tags":[13,16,14,15],"class_list":["post-54","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript","tag-generate","tag-javascriptphaser","tag-random","tag-string"],"yoast_head":"\nGenerate a random string in Javascript\/Phaser - Phaser Games<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/phasergames.com\/generate-a-random-string-in-javascriptphaser\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Generate a random string in Javascript\/Phaser - Phaser Games\" \/>\n<meta property=\"og:description\" content=\"One of the thing I\u2019ve had to do commonly over the years in making games is to be able to generate a random string. Now lot of times I do this on the server side of code to be able to generate a none, one time use code. But there are times where I have […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/phasergames.com\/generate-a-random-string-in-javascriptphaser\/\" \/>\n<meta property=\"og:site_name\" content=\"Phaser Games\" \/>\n<meta property=\"article:published_time\" content=\"2017-05-17T21:45:46+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-05-17T22:16:51+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/media.publit.io\/file\/w_900,h_400,c_fit,q_80\/websiteImages\/phasergames\/15547-nq53j8-900x400.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"400\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"William\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"William\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/phasergames.com\/generate-a-random-string-in-javascriptphaser\/\",\"url\":\"https:\/\/phasergames.com\/generate-a-random-string-in-javascriptphaser\/\",\"name\":\"Generate a random string in Javascript\/Phaser - Phaser Games\",\"isPartOf\":{\"@id\":\"https:\/\/phasergames.com\/#website\"},\"datePublished\":\"2017-05-17T21:45:46+00:00\",\"dateModified\":\"2017-05-17T22:16:51+00:00\",\"author\":{\"@id\":\"https:\/\/phasergames.com\/#\/schema\/person\/b434f42d364f46cafe18233abe7db951\"},\"breadcrumb\":{\"@id\":\"https:\/\/phasergames.com\/generate-a-random-string-in-javascriptphaser\/#breadcrumb\"},\"inLanguage\":\"en\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/phasergames.com\/generate-a-random-string-in-javascriptphaser\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/phasergames.com\/generate-a-random-string-in-javascriptphaser\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/phasergames.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Generate a random string in Javascript\/Phaser\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/phasergames.com\/#website\",\"url\":\"https:\/\/phasergames.com\/\",\"name\":\"Phaser Games\",\"description\":\"Tutorials Code and Resources\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/phasergames.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/phasergames.com\/#\/schema\/person\/b434f42d364f46cafe18233abe7db951\",\"name\":\"William\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en\",\"@id\":\"https:\/\/phasergames.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6dc3c530c4e0428b7d6f496d147d5b29?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6dc3c530c4e0428b7d6f496d147d5b29?s=96&d=mm&r=g\",\"caption\":\"William\"},\"url\":\"https:\/\/phasergames.com\/author\/admin\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Generate a random string in Javascript\/Phaser - Phaser Games","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/phasergames.com\/generate-a-random-string-in-javascriptphaser\/","og_locale":"en_US","og_type":"article","og_title":"Generate a random string in Javascript\/Phaser - Phaser Games","og_description":"One of the thing I\u2019ve had to do commonly over the years in making games is to be able to generate a random string. Now lot of times I do this on the server side of code to be able to generate a none, one time use code. But there are times where I have […]","og_url":"https:\/\/phasergames.com\/generate-a-random-string-in-javascriptphaser\/","og_site_name":"Phaser Games","article_published_time":"2017-05-17T21:45:46+00:00","article_modified_time":"2017-05-17T22:16:51+00:00","og_image":[{"width":900,"height":400,"url":"https:\/\/media.publit.io\/file\/w_900,h_400,c_fit,q_80\/websiteImages\/phasergames\/15547-nq53j8-900x400.jpg","type":"image\/jpeg"}],"author":"William","twitter_card":"summary_large_image","twitter_misc":{"Written by":"William","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/phasergames.com\/generate-a-random-string-in-javascriptphaser\/","url":"https:\/\/phasergames.com\/generate-a-random-string-in-javascriptphaser\/","name":"Generate a random string in Javascript\/Phaser - Phaser Games","isPartOf":{"@id":"https:\/\/phasergames.com\/#website"},"datePublished":"2017-05-17T21:45:46+00:00","dateModified":"2017-05-17T22:16:51+00:00","author":{"@id":"https:\/\/phasergames.com\/#\/schema\/person\/b434f42d364f46cafe18233abe7db951"},"breadcrumb":{"@id":"https:\/\/phasergames.com\/generate-a-random-string-in-javascriptphaser\/#breadcrumb"},"inLanguage":"en","potentialAction":[{"@type":"ReadAction","target":["https:\/\/phasergames.com\/generate-a-random-string-in-javascriptphaser\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/phasergames.com\/generate-a-random-string-in-javascriptphaser\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/phasergames.com\/"},{"@type":"ListItem","position":2,"name":"Generate a random string in Javascript\/Phaser"}]},{"@type":"WebSite","@id":"https:\/\/phasergames.com\/#website","url":"https:\/\/phasergames.com\/","name":"Phaser Games","description":"Tutorials Code and Resources","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/phasergames.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en"},{"@type":"Person","@id":"https:\/\/phasergames.com\/#\/schema\/person\/b434f42d364f46cafe18233abe7db951","name":"William","image":{"@type":"ImageObject","inLanguage":"en","@id":"https:\/\/phasergames.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/6dc3c530c4e0428b7d6f496d147d5b29?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6dc3c530c4e0428b7d6f496d147d5b29?s=96&d=mm&r=g","caption":"William"},"url":"https:\/\/phasergames.com\/author\/admin\/"}]}},"featured_image_src":"https:\/\/media.publit.io\/file\/w_600,h_400,c_fill,q_80\/websiteImages\/phasergames\/15547-nq53j8-900x400.jpg","featured_image_src_square":"https:\/\/media.publit.io\/file\/w_600,h_600,c_fill,q_80\/websiteImages\/phasergames\/15547-nq53j8-900x400.jpg","author_info":{"display_name":"William","author_link":"https:\/\/phasergames.com\/author\/admin\/"},"_links":{"self":[{"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/posts\/54"}],"collection":[{"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/comments?post=54"}],"version-history":[{"count":1,"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/posts\/54\/revisions"}],"predecessor-version":[{"id":64,"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/posts\/54\/revisions\/64"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/media\/86"}],"wp:attachment":[{"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/media?parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/categories?post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phasergames.com\/wp-json\/wp\/v2\/tags?post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}