June 23, 2009

Generate Random Strings Using ASP/VBScript

This is a simple yet highly customizable function that allows you to generate random strings using VBScript that you can use in your ASP/VBScript applications. The random strings can be used for various purposes, including:

  • Login Systems
    • Random password for new registrations
    • Random password for users requesting password reset
  • Referral Codes
  • Promotional Codes
  • Primary Keys for tables where using integers as primary keys is not desirable

The following code is a VBScript translation of the php random string function I wrote earlier. It generates an 8 character long random string that contains 1 digit:

<%
  function RandomString( )

    Randomize( )

    dim CharacterSetArray
    CharacterSetArray = Array( _
      Array( 7, "abcdefghijklmnopqrstuvwxyz" ), _
      Array( 1, "0123456789" ) _
    )

    dim i
    dim j
    dim Count
    dim Chars
    dim Index
    dim Temp

    for i = 0 to UBound( CharacterSetArray )

      Count = CharacterSetArray( i )( 0 )
      Chars = CharacterSetArray( i )( 1 )

      for j = 1 to Count

        Index = Int( Rnd( ) * Len( Chars ) ) + 1
        Temp = Temp & Mid( Chars, Index, 1 )

      next

    next

    dim TempCopy

    do until Len( Temp ) = 0

      Index = Int( Rnd( ) * Len( Temp ) ) + 1
      TempCopy = TempCopy & Mid( Temp, Index, 1 )
      Temp = Mid( Temp, 1, Index - 1 ) & Mid( Temp, Index + 1 )

    loop

    RandomString = TempCopy

  end function
%>
Sample Output
jogwafs2
uuwigi6r
cj3gpdbn
7ifmzdhq
yvceae4x
qm8dxhik
4kyhyevr
evk9hmcb

For better readability and ease of customization, I have split the CharacterSetArray definition on to multiple lines. This code relies on VBScript Arrays and the following functions:

  • Randomize( )
  • Rnd( )
  • Int( )
  • Len( )
  • Mid( )

Customization

With minor changes to the CharacterSetArray, you can customize the length and characters of the generated strings.

Complex password that contains 1 digit and 1 symbol
<%
  function RandomString( )

    Randomize( )

    dim CharacterSetArray
    CharacterSetArray = Array( _
      Array( 6, "abcdefghijklmnopqrstuvwxyz" ), _
      Array( 1, "0123456789" ), _
      Array( 1, "!@#$+-*&?:" ) _
    )

    dim i
    dim j
    dim Count
    dim Chars
    dim Index
    dim Temp

    for i = 0 to UBound( CharacterSetArray )

      Count = CharacterSetArray( i )( 0 )
      Chars = CharacterSetArray( i )( 1 )

      for j = 1 to Count

        Index = Int( Rnd( ) * Len( Chars ) ) + 1
        Temp = Temp & Mid( Chars, Index, 1 )

      next

    next

    dim TempCopy

    do until Len( Temp ) = 0

      Index = Int( Rnd( ) * Len( Temp ) ) + 1
      TempCopy = TempCopy & Mid( Temp, Index, 1 )
      Temp = Mid( Temp, 1, Index - 1 ) & Mid( Temp, Index + 1 )

    loop

    RandomString = TempCopy

  end function
%>
Sample Output
zb5&bxjr
pd$8lrlu
yljmoq:5
$xsaxc6r
sf!n3mho
tpg+t4pr
@zykc1pb
ln3ep-vk
Even more complex password that contains 1 upper-case character, 1 digit and 1 symbol
<%
  function RandomString( )

    Randomize( )

    dim CharacterSetArray
    CharacterSetArray = Array( _
      Array( 5, "abcdefghijklmnopqrstuvwxyz" ), _
      Array( 1, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ), _
      Array( 1, "0123456789" ), _
      Array( 1, "!@#$+-*&?:" ) _
    )

    dim i
    dim j
    dim Count
    dim Chars
    dim Index
    dim Temp

    for i = 0 to UBound( CharacterSetArray )

      Count = CharacterSetArray( i )( 0 )
      Chars = CharacterSetArray( i )( 1 )

      for j = 1 to Count

        Index = Int( Rnd( ) * Len( Chars ) ) + 1
        Temp = Temp & Mid( Chars, Index, 1 )

      next

    next

    dim TempCopy

    do until Len( Temp ) = 0

      Index = Int( Rnd( ) * Len( Temp ) ) + 1
      TempCopy = TempCopy & Mid( Temp, Index, 1 )
      Temp = Mid( Temp, 1, Index - 1 ) & Mid( Temp, Index + 1 )

    loop

    RandomString = TempCopy

  end function
%>
Sample Output
cg9hm$Yy
Mmp&j1kp
kipww7H+
?oVjny9b
Qjq5lgi-
sd:7Elnp
*kaxgl4Z
xqjc5jN!
Referral code containing mix of digits and upper-case characters
<%
  function RandomString( )

    Randomize( )

    dim CharacterSetArray
    CharacterSetArray = Array( _
      Array( 4, "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ), _
      Array( 4, "0123456789" ) _
    )

    dim i
    dim j
    dim Count
    dim Chars
    dim Index
    dim Temp

    for i = 0 to UBound( CharacterSetArray )

      Count = CharacterSetArray( i )( 0 )
      Chars = CharacterSetArray( i )( 1 )

      for j = 1 to Count

        Index = Int( Rnd( ) * Len( Chars ) ) + 1
        Temp = Temp & Mid( Chars, Index, 1 )

      next

    next

    dim TempCopy

    do until Len( Temp ) = 0

      Index = Int( Rnd( ) * Len( Temp ) ) + 1
      TempCopy = TempCopy & Mid( Temp, Index, 1 )
      Temp = Mid( Temp, 1, Index - 1 ) & Mid( Temp, Index + 1 )

    loop

    RandomString = TempCopy

  end function
%>
Sample Output
9HL284CB
OTH357O8
DY6W32J0
4KI134MJ
QP190HE9
KDU35A67
PY03F3S2
3EBV871R

0 comments: