{"id":424,"date":"2009-02-05T13:10:00","date_gmt":"2009-02-05T13:10:00","guid":{"rendered":"http:\/\/www.mjtnet.com\/blog\/?p=424"},"modified":"2009-02-05T13:10:00","modified_gmt":"2009-02-05T13:10:00","slug":"generating-random-characters-and-strings","status":"publish","type":"post","link":"https:\/\/www.mjtnet.com\/blog\/2009\/02\/05\/generating-random-characters-and-strings\/","title":{"rendered":"Generating Random Characters and Strings"},"content":{"rendered":"<p><a href=\"http:\/\/www.mjtnet.com\/macro_scheduler.htm\">Macro Scheduler<\/a> has a Random function which will give you a random number between a specified range.  Every now and then I am asked how you can create a random character or random character string.  <\/p>\n<p>The solution is to use VBScript&#8217;s Chr function which returns the character for the specified Ascii code.  So we can use Random to get a random number within the required Ascii range, and then use Chr to get the corresponding character.  There are a few examples in the forums, such as <a href=\"http:\/\/www.mjtnet.com\/usergroup\/viewtopic.php?p=21420\">this one<\/a>.  <\/p>\n<p>I was recently asked how to generate a random character from A to Z.   Here&#8217;s my response:<\/p>\n<p>Look at the ASCII table: <a href=\"http:\/\/www.asciitable.com\/\">http:\/\/www.asciitable.com\/<\/a><\/p>\n<p>Let&#8217;s look at upper case only first:<\/p>\n<p>A is ascii code decimal 65<br \/>\nZ is ascii code decimal 90<\/p>\n<p>So if we want a random character from A-Z inclusive we want to get a random number in that range (65 to 90).<\/p>\n<p>Clearly it&#8217;s a range of 26. Random gives a value from 0 to range-1. So:<\/p>\n<pre name=\"code\" class=\"macroscript\">Random>26,random_number<\/pre>\n<p>So to get that into our range we clearly need to add it to 65, because the ascii range begins at 65 for &#8220;A&#8221;:<\/p>\n<pre name=\"code\" class=\"macroscript\">Let>ascii_code=65+random_number<\/pre>\n<p>Now we have an ascii code in the required range. So now use VBScript&#8217;s Chr function which returns the corresponding character for the ascii code:<\/p>\n<pre name=\"code\" class=\"macroscript\">VBEval>chr(%ascii_code%),random_char<\/pre>\n<p>So, in long form:<\/p>\n<pre name=\"code\" class=\"macroscript\">Random>26,random_number\r\nLet>ascii_code=65+random_number\r\nVBEval>chr(%ascii_code%),random_char<\/pre>\n<p>Or, shorter version:<\/p>\n<pre name=\"code\" class=\"macroscript\">Random>26,random_number\r\nVBEval>chr(%random_number%+65),random_char<\/pre>\n<p>That will give you a random character between A and Z inclusive.<\/p>\n<p>If you want lower case then the range is 97 &#8211; 122.  There are some characters in between. So if you want to mix lower case and upper case, you could either look at the entire range, discarding any results that appear between the two, or throw a coin (Random>2) to determine whether to do the upper case range or the lower case range, or, I think simpler, randomly convert to lower case, e.g.:<\/p>\n<pre name=\"code\" class=\"macroscript\">Random>26,random_number\r\nVBEval>chr(%random_number%+65),random_char\r\n\r\nRandom>2,UpperOrLower\r\nIf>UpperOrLower=0\r\n  VBEval>LCase(\"%random_char%\"),random_char\r\nEndif\r\n\r\nMessageModal>random_char<\/pre>\n<p>Remember, to use VBScript you need a VBSTART\/VBEND block in your script, even if it is empty.  Stick it at the top.  So the whole example script becomes:<\/p>\n<pre name=\"code\" class=\"macroscript\">VBSTART\r\nVBEND\r\n\r\nRandom>26,random_number\r\nVBEval>chr(%random_number%+65),random_char\r\n\r\nRandom>2,UpperOrLower\r\nIf>UpperOrLower=0\r\n  VBEval>LCase(\"%random_char%\"),random_char\r\nEndif\r\n\r\nMessageModal>random_char<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Macro Scheduler has a Random function which will give you a random number between a specified range. Every now and then I am asked how you can create a random character or random character string. The solution is to use VBScript&#8217;s Chr function which returns the character for the specified Ascii code. So we can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6],"tags":[],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/424"}],"collection":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/comments?post=424"}],"version-history":[{"count":4,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/424\/revisions"}],"predecessor-version":[{"id":428,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/posts\/424\/revisions\/428"}],"wp:attachment":[{"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/media?parent=424"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/categories?post=424"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mjtnet.com\/blog\/wp-json\/wp\/v2\/tags?post=424"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}