{"id":99,"date":"2013-09-10T14:18:23","date_gmt":"2013-09-10T12:18:23","guid":{"rendered":"http:\/\/candrea.ch\/blog\/?p=99"},"modified":"2013-09-10T14:22:20","modified_gmt":"2013-09-10T12:22:20","slug":"the-beauty-of-unique-alphanumeric-random-codes","status":"publish","type":"post","link":"https:\/\/candrea.ch\/blog\/the-beauty-of-unique-alphanumeric-random-codes\/","title":{"rendered":"The Beauty of Unique Alphanumeric Random Codes"},"content":{"rendered":"<p>My challenge was to generate random codes of variable length and size. The codes should be all unique and build of alphanumeric symbols. I wrote the following function using the build-in function sample():<\/p>\n<pre class=\"lang:default decode:true\" title=\"alpha.rnd(length, size)\">alpha.rnd &lt;- function(length, size) {\r\n\tif(size &gt; 36^length) stop(\"\\n size cannot be greater than 36^length\")\r\n\tb &lt;- NULL\r\n\trepeat {\r\n\t\ta &lt;- NULL\t\r\n\t\tfor (i in 1:length) {\r\n\t\t\ta &lt;- paste0(sample(c(letters, 0:9), size, replace = TRUE), a)\r\n\t\t}\r\n\t\tb &lt;- c(a,b)\r\n\t\tif(sum(duplicated(b)) == 0) break\r\n\t  \tsize = sum(duplicated(b))\r\n\t\tb &lt;- b[!duplicated(b)]\t\t\r\n\t}\r\n\treturn(b)\r\n}<\/pre>\n<p>Example:<\/p>\n<pre class=\"lang:default decode:true\" title=\"Example\">&gt; set.seed(77)\r\n&gt; alpha.rnd(3,5)\r\n[1] \"5qk\" \"94z\" \"f45\" \"au8\" \"qq0\"<\/pre>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>My challenge was to generate random codes of variable length and size. The codes should be all unique and build of alphanumeric symbols. I wrote the following function using the build-in function sample(): alpha.rnd &lt;- function(length, size) { if(size &gt; 36^length) stop(&#8220;\\n size cannot be greater than 36^length&#8221;) b &lt;- NULL repeat { a &lt;- &hellip; <a href=\"https:\/\/candrea.ch\/blog\/the-beauty-of-unique-alphanumeric-random-codes\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">The Beauty of Unique Alphanumeric Random Codes<\/span> <span class=\"meta-nav\">&rarr;<\/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":[3],"tags":[],"class_list":["post-99","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/posts\/99"}],"collection":[{"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/comments?post=99"}],"version-history":[{"count":5,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/posts\/99\/revisions"}],"predecessor-version":[{"id":104,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/posts\/99\/revisions\/104"}],"wp:attachment":[{"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/media?parent=99"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/categories?post=99"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/candrea.ch\/blog\/wp-json\/wp\/v2\/tags?post=99"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}