Random String

Posted on 2000-03-11
Last Modified: 2010-05-02
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.
Question by:vbRetard
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 2608555
Randomize Timer
RandInt = Int((UBound(s) - LBound(s) + 1) * Rnd + LBound(s))

NewRandomString = s(RandInt)

Accepted Solution

CoolAss earned 20 total points
ID: 2608557
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.
LVL 14

Expert Comment

ID: 2608950
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 ----->
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 14

Expert Comment

ID: 2608959
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>.


Expert Comment

ID: 2609922
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.

Expert Comment

ID: 2610420
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>
LVL 14

Expert Comment

ID: 2610959
"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..


Expert Comment

ID: 2611023
Um. Ok.

I will comment more. :-p
LVL 14

Expert Comment

ID: 2611101
a Tongue out?.. <laughing>.. Thank YOU.. YOU won't regret it.. <smile>.

If perhaps I win this question, PLEASE give the points to Coolass.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question