Random String

Hi I am trying to get a random string but i don't know how to do this. I tried to create a simple array and then to get a string out of it. Does anyone can help me on this ? Here is what i got ...
s = Array("string1","string2","string3")
now i want to have a random string. Please tell me how i can do this. Thanks.
Who is Participating?
CoolAssConnect With a Mentor Commented:
An easy way to do this would be:

      For i = 1 to 8
            intNum = Int(10 * Rnd + 48)
            intUpper = Int(26 * Rnd + 65)
            intLower = Int(26 * Rnd + 97)
            intRand = Int(3 * Rnd + 1)
            Select Case intRand
                  Case 1
                        strPartStr = Chr(intNum)
                  Case 2
                        strPartStr = Chr(intUpper)
                  Case 3
                        strPartStr = Chr(intLower)
            End Select
            GenStr = GenStr & strPartStr      

This will create a random string of numbers and letters... you could also modify it to create a random string of just numbers or just letters.
Randomize Timer
RandInt = Int((UBound(s) - LBound(s) + 1) * Rnd + LBound(s))

NewRandomString = s(RandInt)
The following function will do what you request. The function syntax is..

StringVariable = RANDOMSTRING (LengthOfString, <optional> StringCase)

<----- Code Begin ----->

Public Function RandomString _
(ByVal lngLength As Long, _
Optional ByVal varCase As Variant) _
As String

    Const MIXED_CASE = 0
    Const LOWER_CASE = 1
    Const UPPER_CASE = 2
    If IsMissing(varCase) _
        varCase = UPPER_CASE
    End If
    Dim strCharacters As String ' Add any Characters you want to this string
    strCharacters _
        & "abcdefghijklmnopqstuvwxyz"
    Dim lngIndex As Long
    Dim lngWork As Long
    Dim strWork As String
    For lngIndex = 0 To lngLength - 1
        lngWork = ((CSng(Len(strCharacters)) - 1) * Rnd) + 1
        strWork = strWork & Mid$(strCharacters, lngWork, 1)
    Next lngIndex

    Select Case varCase
        Case UPPER_CASE
            RandomString = UCase(strWork)
        Case LOWER_CASE
            RandomString = LCase(strWork)
        Case Else
            RandomString = strWork
    End Select

End Function

<----- Code End ----->
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Ahhhh.. my ole "friend" Coolass.. locking down questions again.. as HIS can be the only answer. As Coolass puts it.. it would be silly to think that anyone else need comment.. Welp.. here I am.. <lol>.

Good Luck Coolass.. <smile>.

I don't know what your problem is wsh2, but I really wish you would leave me alone.

I have every right to post an answer, as do you.
Boys.. boys... calm down.

whs2 is correct... coolass - please only lock a question if you are sure you know the definitive answer to a question. If your ass is as cool as you say it is your T-shirt will come eventually.

Meanwhile... be fair to the questioner - you can get points for a comment if it is helpful. There's nothing to be gained by locking questions because comments can be accepted an answers anyway ! The raison d'etre for this forum is to share knowledge, not to provide answers as quickly as possible.

NB wsh2... nice to see you are using the optimal version of MID$... you know how important it is to optimise code <grin>
"you know how important it is to optimise code".. Yeah.. like I am the Clint Eastwood of code.. <LOL>. But thanks for the compliment.. <smile>.

I find your coding solutions always VERY accurate and right on the cutting edge of Visual Basic technology. As such, I respect your solutions, knowledge and expertise very much.. and am sincerey happy that you take your valuable time and ADD to this, the Experts Exchange community.. <smile>.

To be honest.. I chide you to comment.. only to see that you too benefit from your Experts Exchange endeavor. As I have already indicated, every answer of yours I have read has been very, very competent. But there is more to the art of programming than just technical competence. To really be good at this craft, you have to listen to your user, to read between the lines of what they write or say, in order to give them  what they really want.

In many ways, the true value of Experts Exchange, is in learning how to sell our creative technical ideas. Experts Exchange brings the customers in, and like a lot full of used car salesmen, we set upon them.. <Theme to Jaws now playing>.. LOL. Unfortunately , the customers are a varied lot.. from Experts themselves all the way down to this week's homework project due in the morning.. <sheesh>. Regardless, of their level, give them what they want, is always the key.. to be successful in programming.. salesmanship is still king.

Don't get me wrong.. I can often appreciate how you feel, after spending an hour scripting a letter perfect response only to find that the bozo I am writing too, just wanted help on something completely unrelated, I too want to change the laws so that Tigers can once again eat their young if they care too. But that is people, and it's something we all have to live with.. that they are all different, is something wonderful and valuable to be learned.

Coolass, as any Expert would agree, your contributions are impeccible and most worthy of posting. However to truly be good at this profession, the secret of the game, the art, the reward.. the fun.. is in learning how to listen.. and in giving the customer NOT necessarily  what we think is best.. but giving the customer what he/she truly wants.

You are very good Coolass, rise to the challenge of commenting.. and you will find that you will only become better.
Both from the feedback of fellow experts, and in satisfying the needs of a customer.. each a reward to itself.

As for me, I have always appreciated your technical thoughts and comments.. and continue to do so.. I only chide you because you lock them away so quickly.. and for not partaking in what other experts are only to willing to share with you. Long story short.. I can't wait until you get the T-shirt.. <LOL>.. You truly are deserving.. <smile>.

ALL the best to ya..

Um. Ok.

I will comment more. :-p
a Tongue out?.. <laughing>.. Thank YOU.. YOU won't regret it.. <smile>.

If perhaps I win this question, PLEASE give the points to Coolass.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.