PDA

View Full Version : Generate random number of strings that are stored in an array .



wannabe11
10-11-2017, 09:09 AM
Hello guys

following up with my vba journey , i'm facing a problem which is how to generate n number of strings that are stored in array , each one generated being unique.

my concern as for now is to just generate the n strings first and then work with the code to obtain the uniqueness of generated strings .

i was able to create a function that generate a random number within a certain range but for strings i'm stuck , i'v done some research and i cant seem to find the answer .

i thought that maybe i have to convert the strings to numerical value using a function and then generate the n amount and then convert them back to strings but i'm unable to apply this idea .

thanks for your answers and help in advance .

Paul_Hossler
10-11-2017, 09:37 AM
How long is each string, or does it vary -- max and min length

What character set, A-Z or A-z or 0-9A-z or any printable character

JKwan
10-11-2017, 09:38 AM
give this a go

Sub GenRandomStrings()
Const NumberOfLettersPerString As Long = 4
Const HowManyWords As Long = 10
Dim NumberOfTimes As Long
Dim RandowString As String

For i = 1 To HowManyWords
For NumberOfTimes = 1 To NumberOfLettersPerString
RandowString = RandowString & GetLetter
Next NumberOfTimes
Cells(i, "A") = RandowString
RandowString = ""
Next i
End Sub
Function GetLetter() As String
Dim bOK As Boolean
Dim a As Double

bOK = False
Do
Randomize
a = Int((122 * Rnd) + 1)
Select Case a
Case 65 To 90, 97 To 122
GetLetter = Chr(a)
bOK = True
End Select
Loop Until bOK
End Function

wannabe11
10-11-2017, 09:44 AM
basically all the strings have the same length which is 8 characters , from A to Z and from 0 to 9 for the numbers .

wannabe11
10-11-2017, 09:45 AM
Thanks for your answer sir , can you please summarize or describe how the function GetLetter work ?

wannabe11
10-11-2017, 09:59 AM
Sub randomstrings()
Dim i As Integer
Dim stringarray(1 To 100) As String


For i = 1 To 100
stringarray(i) = "STOCK" & i + 100
Next i

For i = 1 To 20
Randomize
stringarray(i) = stringarray(Int((UBound(stringarray) - LBound(stringarray) + 1) * Rnd + LBound(stringarray)))

Next i


'Test
msg = ""
For i = 1 To 10
msg = msg & " " & stringarray(i)
Next i
MsgBox msg
End Sub



thanks for your answers guys , i've came across an example online and i modified the code a bit . it seems to work fine .
still i dont understand the role of the randomize and how the int function solve the issue since this function only bring whole numbers .

JKwan
10-11-2017, 11:10 AM
Randomize - just to seed the rnd function, otherwise, you get the same random number
INT function is to return the whole number portion of a double.

Paul_Hossler
10-11-2017, 12:50 PM
thanks for your answers guys , i've came across an example online and i modified the code a bit . it seems to work fine .
still i dont understand the role of the randomize and how the int function solve the issue since this function only bring whole numbers .


, each one generated being unique.

That macro doesn't guarantee uniqueness



Option Explicit

Const N As Long = 10000
Const strLength As Long = 8
Const strChars As String = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"

Sub UniqueStrings()
Dim r As Long
Dim s As String, s1 As String
Dim v As Variant
Dim i As Long

'add first string and a seperation char
s = pvtMakeString(strLength, strChars) & "#"

For i = 2 To N
'make another
s1 = pvtMakeString(strLength, strChars)

'check if already in the string list
Do While InStr(s, s1) > 0
'if it is try another until it's not
s1 = pvtMakeString(strLength, strChars)
Loop

s = s & s1 & "#"
Next i

'split string at the #
v = Split(s, "#")

For i = 0 To N - 1
Cells(i + 1, 1) = v(i)
Next i
End Sub


Private Function pvtMakeString(L As Long, C As String) As String
Dim i As Long
Dim r As Long
Dim s As String

For i = 1 To L
r = Int(Len(strChars) * Rnd + 1)
s = s & Mid(C, r, 1)
Next i
pvtMakeString = s
End Function