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 7016843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:50:17+00:00 2026-05-27T22:50:17+00:00

Currently my program (see below) generates random strings (made of numbers) from 8 –

  • 0

Currently my program (see below) generates random strings (made of numbers) from 8 – 12 characters in length.

public static string GenerateNewCode(int CodeLength)
{
    string newCode = String.Empty;
    int seed = unchecked(DateTime.Now.Ticks.GetHashCode());
    Random random = new Random(seed);

    // keep going until we find a unique code       `
    do
    {
        newCode = random.Next(Convert.ToInt32(Math.Pow(10, CodeLength - 1)), Convert.ToInt32(Math.Pow(10, CodeLength) - 1)).ToString("0000");
    }
    while (!ConsumerCode.isUnique(newCode));

    // return
    return newCode;
}

However, their is a problem with the method, when the codeLength is 10 or greater it causes error because 109 is greater than int32.MaxValue.

Not sure how to get around this issue.

  • 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-27T22:50:18+00:00Added an answer on May 27, 2026 at 10:50 pm

    Your code doing the same thing without some freak lines:

    public static string GenerateNewCode(int CodeLength)
    {
        Random random = new Random();
        StringBuilder output = new StringBuilder();
    
        do
        {   
            for (int i = 0; i < CodeLength; i++)
            {
                output.Append(random.Next(0, 10));
            }
        }
        while (!ConsumerCode.isUnique(output.ToString()));
        return output.ToString();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Currently my program generates random 8 character strings made from numbers. See below public
I have a program below that tries to take input from the user and
In the code provided below, you will see a map made of various tiles.
I'm writing a simple program - please see below for my code with comments.
Currently my program is in a spot where it both listens for the user
I'm currently designing a program that will involve some physics (nothing too fancy, a
I am currently trying to program my first ajax interface using Rails. The application
I'm currently writing a program to generate really enormous (65536x65536 pixels and above) Mandelbrot
I am currently developing a program that uses C#'s Dictionary container (specifically, SortedDictionary). This
The program that I am currently assigned to has a requirement that I copy

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.