gpt4 book ai didi

vb.net - 在 vb.net 中生成随机字符串

转载 作者:行者123 更新时间:2023-12-02 02:08:12 25 4
gpt4 key购买 nike

我需要在 vb.net 中生成随机字符串,该字符串必须由(随机选择的)字母 A-Z(必须大写)和散布的随机数字组成。它还需要能够生成具有设定长度的它们。

感谢您的帮助,这让我发疯!

最佳答案

如果您可以将其转换为 VB.NET(这很简单),我想说您就可以开始了(如果您不能,请使用 this 或任何其他有值(value)的工具):

/// <summary>
/// The Typing monkey generates random strings - can't be static 'cause it's a monkey.
/// </summary>
/// <remarks>
/// If you try hard enough it will eventually type some Shakespeare.
/// </remarks>
class TypingMonkey
{
private const string legalCharacters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";

static Random random = new Random();

/// <summary>
/// The Typing Monkey Generates a random string with the given length.
/// </summary>
/// <param name="size">Size of the string</param>
/// <returns>Random string</returns>
public string TypeAway(int size)
{
StringBuilder builder = new StringBuilder();
char ch;

for (int i = 0; i < size; i++)
{
ch = legalCharacters[random.Next(0, legalCharacters.Length)];
builder.Append(ch);
}

return builder.ToString();
}
}

那么您所要做的就是:

TypingMonkey myMonkey = new TypingMonkey();
string randomStr = myMonkey.TypeAway(size);

关于vb.net - 在 vb.net 中生成随机字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1546472/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com