Solved

Random String

Posted on 2000-03-11
9
194 Views
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.
0
Comment
Question by:vbRetard
9 Comments
 
LVL 5

Expert Comment

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

NewRandomString = s(RandInt)
0
 
LVL 4

Accepted Solution

by:
CoolAss earned 20 total points
ID: 2608557
An easy way to do this would be:

      Randomize
      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      
Next

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.
0
 
LVL 14

Expert Comment

by:wsh2
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) _
    Then
        varCase = UPPER_CASE
    End If
   
    Dim strCharacters As String ' Add any Characters you want to this string
    strCharacters _
        = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" _
        & "abcdefghijklmnopqstuvwxyz"
   
    Dim lngIndex As Long
    Dim lngWork As Long
    Dim strWork As String
    Randomize
    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 ----->
0
 
LVL 14

Expert Comment

by:wsh2
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>.

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Expert Comment

by:CoolAss
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.
0
 
LVL 3

Expert Comment

by:paulstamp
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>
0
 
LVL 14

Expert Comment

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

Coolass:
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..

B.
0
 
LVL 4

Expert Comment

by:CoolAss
ID: 2611023
Um. Ok.

I will comment more. :-p
0
 
LVL 14

Expert Comment

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

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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

863 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now