Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7607607
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T00:42:25+00:00 2026-05-31T00:42:25+00:00

I’m trying to generate 900,000 keys and store them to a db. Each value

  • 0

I’m trying to generate 900,000 keys and store them to a db.

Each value has to be 8 chars and different from generated ones.

Here is my code look like, it’s doing ok but it’s too slow…

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();
        }
    }
}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T00:42:27+00:00Added an answer on May 31, 2026 at 12:42 am

    Well to start with, I would generate them all and then put them into the UI. I also wouldn’t use string.IndexOf to find if you’ve already got a match. Use a HashSet<string>:

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

    You also shouldn’t be setting CheckForIllegalCrossThreadCalls to false. Just generate them all in the background and then use Invoke to get to the UI thread to update your UI. If you really want to update as you go, you could do so periodically… although I wouldn’t put them all into a single text box.

    Next, I’d look at the RandomString method. Do you really need to create a new RNGCryptoServiceProvider on every iteration? Why not create one and then reuse it?

    Likewise, why bother creating a new HashSet<char> and calling ToArray on every call? It sounds like you should probably have a separate class as a RandomCodeGenerator which remembers:

    • The size of code you’re generating
    • The random number generator
    • The allowed characters

    That will let you get away from doing a lot of setup work on every iteration.

    I’d then personally create a char[] of the right size and just iterate until you’ve filled it, then create a string from that… I don’t see the need for StringBuilder… but that’s probably not hurting you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.