The Tale of Two Random String Functions

      No Comments on The Tale of Two Random String Functions

I’ve been thinking a lot of random strings lately. Notice that this isn’t about generating a UUID. I just wanted to generate a reasonably random string. Originally I had need for a user ‘typable’ string. I get really frustrated by the CAPTCHA strings when they include the ambiguous characters of I, 1, O, and 0. (eye, one, oh, and zero) Certain fonts have these characters virtually identical. I’m sure that professional font creators think long and hard about how to design these characters, especially if their purpose is human identifiability. This really frustrates me.

Originally, I was just going to create a random string generation function that avoids these characters. Easy enough to do with the way these functions are written by just removing them from the source character set. But then I stumbled across a JavaScript code that created a human-readable string along the lines of what could be used by CAPTCHA functions.

First, let us look at a string generation function that Pamoxi wrote on the RealSoftware.com forums:

Function RandomString(nStringLength As Integer, IncludeNumbers As Boolean, IncludeLowerCase As Boolean, IncludeUpperCase As Boolean, IncludeSymbols As Boolean) As String
‘ Created by pamoxi from the RealSoftware.com forums!
Dim cSource As String = “”
if IncludeNumbers = True then cSource = cSource + “1234567890”
if IncludeLowerCase = True then cSource = cSource + “abcdefghijklmnopqrstuvwxyz”
if IncludeUpperCase = True then cSource = cSource + “ABCDEFGHIJKLMNOPQRSTUVWXYZ”
if IncludeSymbols = True then cSource = cSource + “|@#~$%()=^*+[]{}-_”
Dim cRetVal, cCharacter As String
Dim nNumberOfCharacters, nStart, nX As Integer
nNumberOfCharacters = cSource.Len( )
for nX = 1 To nStringLength
  Dim oRandom as New Random
  nStart = oRandom.InRange( 1, nNumberOfCharacters )
  cCharacter = cSource.Mid( nStart, 1 )
  cRetVal = cRetVal + cCharacter
Next
return cRetVal
End Function

This function generates strings that look like this:
random_strings1

My original purpose was to create a shorter string that a user could reliably type in, and yet was random. I could use this as a light weight validation that the visitor wasn’t a bot of some type to my website. So I converted the JavaScript function over to RealBasic:

Function RandomStringReadable(nStringLength AS Integer) As String
‘ Created by ThePeppersStudio from http://snipperize.todayclose.com/
‘ Converted to RealBasic by Kevin Cully
DIM cConstanants AS String = “BCDFGHJKLMNPQRSTVWXYZ”
DIM cVowels AS String = “AEIOU”
Dim cRetVal, cCharacter As String = “”
DIM nStart AS Integer
DO UNTIL cRetVal.Len() >= nStringLength
  Dim oRandom as New Random
  nStart = oRandom.InRange( 1, cConstanants.Len() )
  cCharacter = cConstanants.Mid( nStart, 1 )
  cRetVal = cRetVal + cCharacter
  oRandom = New Random
  nStart = oRandom.InRange( 1, cVowels.Len() )
  cCharacter = cVowels.Mid( nStart, 1 )
  cRetVal = cRetVal + cCharacter
LOOP
return cRetVal
End Function

And here is what the output looks like when I ask for a readable random string of 20 characters:
random_strings2

Even when I’m including the characters of what I thought was ambiguous, the human mind is able to identify the pattern and accurately enter the string. The output of this function is superior in readability to most CAPTCHA images. That might be on purpose. Of course it helps eliminating the numbers, including the zero. I hope you find this useful.

Leave a Reply