Randomly Generated Strings

Discussion in 'ASP General' started by Stuart, Feb 18, 2004.

  1. Stuart

    Stuart Guest

    Hi All,

    I used to have a piece of code that would generate a random string of
    8,16,24 or 32 characters in length.

    I got it from this group, does any one have this piece of code?

    It will be used for a double opt-in email newsletter.


    Many thanks,

    Stuart
    Stuart, Feb 18, 2004
    #1
    1. Advertising

  2. It should be something like (out of my head, not tested)

    function GetRandomString(len as integer)
    Dim strLetters as string
    Dim strResult as String
    Dim i as Integer
    strLetters = '01234567890ABCDEFHIJKLMNOPQRSTUVWZYZ'

    For i = 0 to len
    strResult = strResult & SUBSTR(strLetters , FLOOR(RAND() * 36))+1, 1)
    Next
    GetRandomString = strResult
    End function

    --
    Roji. P. Thomas
    SQL Server Programmer
    "Stuart" <> wrote in message
    news:...
    > Hi All,
    >
    > I used to have a piece of code that would generate a random string of
    > 8,16,24 or 32 characters in length.
    >
    > I got it from this group, does any one have this piece of code?
    >
    > It will be used for a double opt-in email newsletter.
    >
    >
    > Many thanks,
    >
    > Stuart
    >
    >
    Roji. P. Thomas, Feb 18, 2004
    #2
    1. Advertising

  3. Stuart

    Stuart Guest

    Many thanks Guys.


    I will save the code away somewhere safe - but not so safe that I cannot
    find it.

    ;o))





    "Peter Foti" <> wrote in message
    news:...
    > "Roji. P. Thomas" <> wrote in message
    > news:...
    > > It should be something like (out of my head, not tested)
    > >
    > > function GetRandomString(len as integer)
    > > Dim strLetters as string
    > > Dim strResult as String
    > > Dim i as Integer
    > > strLetters = '01234567890ABCDEFHIJKLMNOPQRSTUVWZYZ'
    > >
    > > For i = 0 to len
    > > strResult = strResult & SUBSTR(strLetters , FLOOR(RAND() * 36))+1, 1)
    > > Next
    > > GetRandomString = strResult
    > > End function

    >
    > Close, but you need the Randomize statement first. Also, "len" is a
    > keyword. Try this:
    >
    > Function GetRandomString( numChars )
    > Randomize
    > strLetters =
    > "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz"
    > iLettersLen = Len(strLetters)
    > strResult = ""
    >
    > For i = 1 To numChars
    > x = CInt((iLettersLen * Rnd) + 1)
    > strResult = strResult & Mid(strLetters,x,1)
    > next
    >
    > GetRandomString = strResult
    > End Function
    >
    > Regards,
    > Peter Foti
    >
    >
    Stuart, Feb 19, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jimgardener
    Replies:
    1
    Views:
    690
    Arne Vajhøj
    Dec 5, 2008
  2. J1C

    Randomly generated filename.

    J1C, Oct 19, 2005, in forum: ASP General
    Replies:
    7
    Views:
    141
  3. Replies:
    3
    Views:
    147
    Evertjan.
    Jan 15, 2007
  4. woodyee

    Randomly Sorting Strings

    woodyee, Feb 11, 2007, in forum: Ruby
    Replies:
    5
    Views:
    84
    James Edward Gray II
    Feb 11, 2007
  5. Replies:
    4
    Views:
    194
    Brian Candler
    Oct 21, 2009
Loading...

Share This Page