Random strings in ASP

A function in classic ASP VBSCRIPT I wrote to generate one-time key values to be used in a database for access control.

The function createRRKey() takes an integer to define the length of the key to be generated, then returns the created key.
' Create key
Dim RatingKey
RatingKey = createRRKey(25)
Response.Write RatingKey

' Function to create a key
' Picks between lower and higher case letters and numbers from the ASCII table to make a key
Function createRRKey(key_length)
 Dim temp_key, temp_rnd, loop_a

 temp_key = ""
 For loop_a = 1 To key_length

  Randomize
  temp_rnd = Rnd

  If temp_rnd < 0.3 Then
    'a-z
    temp_key = temp_key & Chr(Int(((122-97+1) * Rnd) + 97))
  ElseIf temp_rnd > 0.3 AND temp_rnd < 0.6 Then
    'A-Z
    temp_key = temp_key & Chr(Int(((90-65+1) * Rnd) + 65))
  ElseIf temp_rnd > 0.6 Then
    '0-9
    temp_key = temp_key & Chr(Int(((57-48+1) * Rnd) + 48))
  End If

 Next

 createRRKey = temp_key
End Function


Written by: Dag Jonny Nedrelid
©2007-2012 http://thronic.com


Feel free to leave a comment.
Name:
URL:
0