Consulting

Results 1 to 8 of 8

Thread: Generate random number of strings that are stored in an array .

  1. #1

    Generate random number of strings that are stored in an array .

    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 .

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    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
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    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

  4. #4
    basically all the strings have the same length which is 8 characters , from A to Z and from 0 to 9 for the numbers .

  5. #5
    Thanks for your answer sir , can you please summarize or describe how the function GetLetter work ?

  6. #6
    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 .

  7. #7
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    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.

  8. #8
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,729
    Location
    Quote Originally Posted by wannabe11 View Post
    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
    Last edited by Paul_Hossler; 10-11-2017 at 01:12 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •