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

后端 未结 8 1893
無奈伤痛
無奈伤痛 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: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.

提交回复
热议问题