Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
Solved

# Random String

Posted on 2000-03-11
Medium Priority
212 Views
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
Question by:vbRetard
[X]
###### 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

LVL 5

Expert Comment

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

NewRandomString = s(RandInt)
0

LVL 4

Accepted Solution

CoolAss earned 80 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

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

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

LVL 4

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

LVL 3

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>
0

LVL 14

Expert Comment

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

ID: 2611023
Um. Ok.

I will comment more. :-p
0

LVL 14

Expert Comment

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

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture. Â  There is also a companion Debug Toolbar that looks like the followinâ€¦
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't interesteâ€¦
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applicâ€¦
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â€¦
###### Suggested Courses
Course of the Month9 days, 23 hours left to enroll