create random string of characters

c l
c l used Ask the Experts™
on
hello experts

i need to generate a string of random characters that will be a password for users signing up to my site. after logging in with the random password, they will be forced to change it. I'm still new when it comes to asp, but I know there is a Randomize() function that'll return random numbers. I imagine I'll have to create a character set, then randomly pick different spots within that character set to get a random string of letters/numbers, but I'm not sure how to go about that.

TIA
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Web Ninja at large
Commented:
you're on the right track, you do want a char set to start with and randomly grab different positions in that string to get the random password. Here's a function I've used successfully in my own sites that do exactly what you're looking for. just create a variable and store the results of the function in that variable. any questions feel free to ask.

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

Open in new window

c l

Author

Commented:
thank you, works perfectly!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial