gpt4 book ai didi

c# - 如何使用 C# 生成 8 个字符、900000 个大写字母/数字唯一键?

转载 作者:太空宇宙 更新时间:2023-11-03 19:24:44 24 4
gpt4 key购买 nike

我正在尝试生成 900,000 个 key 并将它们存储到数据库中。

每个值必须是 8 个字符,并且与生成的值不同。

这是我的代码,它运行正常,但速度太慢...



public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Visible = false;
Generate();
}

public int num = 0;

private void Den1()
{
for (int i = 0; i < 900000; i++)
{
string randomStr = RandomString(8);
string textToSearchFor = randomStr;

int index = richTextBox1.Text.IndexOf(textToSearchFor, StringComparison.OrdinalIgnoreCase);

if (index >= 0)
{
MessageBox.Show("Duplicate ignored from 1!");
i--;
}
else
{
richTextBox1.Text += randomStr + "\n";
num++;
}

label1.Text = "" + num;
}

richTextBox1.Visible = true;
}

public void Generate()
{
CheckForIllegalCrossThreadCalls = false;
Thread thread1 = new Thread(Deneme1);

thread1.Start();
}

private string RandomString(int length, string allowedChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
{
if (length < 0) throw new ArgumentOutOfRangeException("length", "length cannot be less than zero.");
if (string.IsNullOrEmpty(allowedChars)) throw new ArgumentException("allowedChars may not be empty.");

const int byteSize = 0x100;
var allowedCharSet = new HashSet<char>(allowedChars).ToArray();
if (byteSize < allowedCharSet.Length) throw new ArgumentException(String.Format("allowedChars may contain no more than {0} characters.", byteSize));

// Guid.NewGuid and System.Random are not particularly random. By using a
// cryptographically-secure random number generator, the caller is always
// protected, regardless of use.
using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider())
{
var result = new StringBuilder();
var buf = new byte[128];
while (result.Length < length)
{
rng.GetBytes(buf);
for (var i = 0; i < buf.Length && result.Length < length; ++i)
{
// Divide the byte into allowedCharSet-sized groups. If the
// random value falls into the last group and the last group is
// too small to choose from the entire allowedCharSet, ignore
// the value in order to avoid biasing the result.
var outOfRangeStart = byteSize - (byteSize % allowedCharSet.Length);
if (outOfRangeStart <= buf[i]) continue;
result.Append(allowedCharSet[buf[i] % allowedCharSet.Length]);
}
}
return result.ToString();
}
}
}

最佳答案

首先,我会生成它们,然后将它们放入 UI。我也不会使用 string.IndexOf查找您是否已经匹配。使用 HashSet<string> :

HashSet<string> codes = new HashSet<string>();
while (codes.Count < 900000)
{
codes.Add(RandomString(8));
}
// Then do what you want with the codes

您也不应该设置CheckForIllegalCrossThreadCalls假的。只需在后台生成它们,然后使用 Invoke进入 UI 线程以更新您的 UI。如果您真的想要随时更新,您可以定期更新...尽管我不会将它们全部放在一个文本框中。

接下来,我会查看 RandomString方法。你真的需要创建一个新的 RNGCryptoServiceProvider 吗?在每次迭代?为什么不创建一个然后重复使用它呢?

同样,为什么要创建一个新的 HashSet<char>并调用 ToArray每次通话?听起来你应该有一个单独的类作为 RandomCodeGenerator它记得:

  • 您生成的代码的大小
  • 随机数生成器
  • 允许的字符

这将使您无需在每次迭代时进行大量设置工作。

然后我会亲自创建一个 char[]大小合适,然后迭代直到填满它,然后从中创建一个字符串...我认为不需要 StringBuilder ......但这可能不会伤害你。

关于c# - 如何使用 C# 生成 8 个字符、900000 个大写字母/数字唯一键?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9632970/

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