且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

生成随机数和字母

更新时间:2023-02-10 12:10:00

要使随机范围正确,您需要确保生成的随机值介于 1 和 VALID_TEXT 字符串的长度之间价值.

For the random range to be correct you need to make sure the random value generated is between 1 and the length of the VALID_TEXT string value.

使用 Rnd() 执行此操作的简单公式是

The simple formula to do this using Rnd() is

(Rnd() * Len(VALID_TEXT)) + 1

还将 Randomize() 移到循环之外,因为这样只会在每次循环迭代时重置种子时降低随机性.

also move Randomize() outside the loop, as it is you'll just make it less random as you're resetting the seed with every iteration of the loop.

错误的原因是Mid() 需要一个有效的开始和大小,零值不是.有关详细信息,请参阅此问题.

可以在另一个问题的此答案中找到有关随机数范围的更多信息.

More information about random number ranges can be found in this answer to another question.