Vb6 How do I make a Random String of 0-9 and a-z of x characters

后端 未结 8 1895
無奈伤痛
無奈伤痛 2021-02-14 03:29

Trying to create a random string, x characters in length using 0-9 and a-z/A-Z and can\'t seem to find a good example, any ideas?

相关标签:
8条回答
  • 2021-02-14 03:41
    Function RandomString(cb As Integer) As String
    
        Randomize
        Dim rgch As String
        rgch = "abcdefghijklmnopqrstuvwxyz"
        rgch = rgch & UCase(rgch) & "0123456789"
    
        Dim i As Long
        For i = 1 To cb
            RandomString = RandomString & Mid$(rgch, Int(Rnd() * Len(rgch) + 1), 1)
        Next
    
    End Function
    

    Please be aware that the built-in random number generator is not cryprographically secure so a function like this should not be used to generate passwords.

    0 讨论(0)
  • 2021-02-14 03:43

    Joel's method is fine (except for the integer loop variable, and using "+" for concatenation). (-:

    However, the output can be made more interesting in a couple of ways.

    First, you can generate strings that have the same approximate frequency distribution as common English text, by creating a seed string with many more Ee and Tt characters than Zz characters. A string of maybe 1000 characters (double that if mixed case) in this approximate mix would work OK.

    Add in equal numbers of 0..9 chars in whatever ratio you would like to see in the final output. You could also shuffle this see string to make it look more random, but it doesn't really matter.

    Then use a random selector in the range of 1..Len(seedstring) to pick each character, just as in Joel's example.

    Why do this? No good reason except that the results will look more familiar.

    A second option is to generate two such seed strings, one of the consonants in corpus weight, and the other with the vowels in the same weighting (more E than O than U, etc). I would use just one case, not mixed case.

    Then alternate two random selections, first from the consonants, then from the vowels, to generate digraphs like TI, WO, DE, and so on. Chain these together to form "words".

    Because the resulting output is pronounceable, it's much more easily remembered. Plus, it looks eerily Japanese. (-:

    Our Stamina library (ASM functions for VB/VBA) has routines that do these things, but it's easy enough in pure VB.

    0 讨论(0)
  • 2021-02-14 03:50

    If you are using SQL database you can generate a PrimaryKey like this:

    SELECT NEWID() as KeyValue

    It's very good way and also very secure.

    0 讨论(0)
  • 2021-02-14 03:51

    You didn't really say what you were using this for. If you need small strings (<=32,766) I think Joel's function will work fine. However if you need something to generate really large strings, this might be useful. On my system it will do a 1,000,000 char string in 0.33291015625 seconds (yes I know... sledgehammer:)) Also you can parametrize the character set so you don't have to change the code every time you want to do something "special":) :

    Public Function RandomString( _
        ByVal length As Long, _
        Optional charset As String = "abcdefghijklmnopqrstuvwxyz0123456789" _
        ) As String
        Dim chars() As Byte, value() As Byte, chrUprBnd As Long, i As Long
        If length > 0& Then
            Randomize
            chars = charset
            chrUprBnd = Len(charset) - 1&
            length = (length * 2&) - 1&
            ReDim value(length) As Byte
            For i = 0& To length Step 2&
                value(i) = chars(CLng(chrUprBnd * Rnd) * 2&)
            Next
        End If
        RandomString = value
    End Function
    
    0 讨论(0)
  • 2021-02-14 04:00

    I forgot all my VB6 (thank God) but in pseudocode it's pretty easy:

        all_chars = an array of all the valid chars
        seed random number generator
        for i = 1 to x do
            random_index = get a random number between 1 and length of all_chars
            'I remember how to concat and comment in VB6 :-)
            string = string & all_chars[random_index] 
        end for
    
        done!
    

    So it's just a matter of finding out how to create an array and fill it with characters, how to get the length of the array and how to get a random number between the first and last indexes of said array.

    Well, all that and looping of course.

    0 讨论(0)
  • 2021-02-14 04:02

    Use Randomize
    Int(Rnd * high bound) + (low bound) will generate a random number
    Generate an array with values from asc("a") to asc("z") and from asc("0") to asc("9")
    Generate random number between 1 and 26 (10+26) and look it up in array.

    Don't have VB6 installed anymore.

    0 讨论(0)
提交回复
热议问题