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

纵饮孤独 提交于 2020-01-12 06:58: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?


回答1:


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.




回答2:


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.




回答3:


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.




回答4:


Inspired by this question I have asked a similar question where I propose using a GUID to generate the sequence. The short coming of this, as I point out, is that if there are no other flaws in my logic then it will be a random sequence of A through F and 10 digits. If you can live with the fact that letters G through Z are missing then this might be a solution for you.




回答5:


Using the algorith of Vinko Vrsalovic, here is the funcionay code, thanks and cya!

all_chars = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","S","T","U","V","W","X","Y","Z")
Randomize
for i = 1 to 4
   random_index = int(Rnd()*25)
clave = clave & all_chars(random_index) 
next



回答6:


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



回答7:


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.




回答8:


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.



来源:https://stackoverflow.com/questions/292254/vb6-how-do-i-make-a-random-string-of-0-9-and-a-z-of-x-characters

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!