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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:20:54+00:00 2026-05-22T01:20:54+00:00

I need to generate a nine digit numeric code (random preferably) which is unique

  • 0

I need to generate a nine digit numeric code (random preferably) which is unique for a given day (same number cannot be generated again on the same day). I was thinking of using HHMMSSmmm (hours, minutes, seconds and milliseconds) to generate the unique code but is not really random. This code generation method can be accessed by multiple methods at the same time so I will have to put a lock on the method. But will this ensure that the number is unique as it is possible that number generation may take less than a millisecond and two threads get the same number?

Is there a better way to generate a random unique numeric code which is unique for a given day? The number of digits can be from 6 to 9 digits.

Edit: The number of random numbers to be generated depends on the number of transactions. Initially the number could be lower but over a period of time it can become very high (multiple transactions per second). Hence, I would not like to compare the number against a used list as this could have performance issues.

Randomness is needed as this number will be entered by the user on the phone. This number is the only way to link the online transaction with the phone transaction so I don’t want user to enter a different number by mistake.

The random number generation needs to take place in a ASP.NET MVC application.

  • 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-22T01:20:54+00:00Added an answer on May 22, 2026 at 1:20 am

    If you start from a random number with 6 digits, then keep adding random, but small enough numbers, you may be abled to do that. You can use the filesystem as the locking storage if you want… but I think you should use a DB for production!

    Here is the sample of what I am talking about:

    This sample is a console application, that uses a file to control concurrency, and to store the last used number and date it was generated.

    If you run it multiple times you will see what happens. Both will have their own unique numbers.

    It will NOT store all generated numbers, like you required!

    This sample can generate for about 999000 random numbers per day, in the range of 6 and 9 digits inclusive. That is about 11 numbers per second.

    using System;
    using System.IO;
    namespace _5893408
    {
        class Program
        {
            static void Main(string[] args)
            {
                Random rand = new Random();
                var futureTime = DateTime.Now.AddSeconds(60);
                while (DateTime.Now < futureTime)
                    Console.WriteLine(GetNextNumber(rand));
            }
    
            public static int GetNextNumber(Random rand)
            {
                var now = DateTime.Now;
                string filePath = @"C:\num.txt";
                FileStream fileStream = null;
                while (fileStream == null)
                {
                    try { fileStream = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None); }
                    catch { }
                }
                using (fileStream)
                {
                    DateTime date;
                    int prevNum;
                    if (fileStream.Length == 0)
                    {
                        date = now;
                        prevNum = rand.Next(100000, 999999);
                    }
                    else
                    {
                        var reader = new StreamReader(fileStream);
                        {
                            date = DateTime.Parse(reader.ReadLine());
                            prevNum = int.Parse(reader.ReadLine());
                        }
                        if (date.DayOfYear != now.DayOfYear)
                            prevNum = rand.Next(100000, 999999);
                    }
                    int nextNum = prevNum + rand.Next(10, 1000);
                    fileStream.Seek(0, SeekOrigin.Begin);
                    using (var writer = new StreamWriter(fileStream))
                    {
                        writer.WriteLine(now);
                        writer.WriteLine(nextNum);
                    }
                    return nextNum;
                }
            }
        }
    }
    

    I think that this fulfills your requirements… am I wrong?

    If I am, just tell and I’ll try to help more.

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

Sidebar

Related Questions

I need to generate a 4 digit random number in C++ I used the
I need to generate a unique code from a single PC, my software will
I need to generate a random number, now this number needs to be somewhere
I need to generate 8 random integers, but they need to be unique, aka
I need to generate a random integer between 1 and n (where n is
I need to generate multiple random values under SQL Server 2005 and somehow this
I need to generate a new interface at run-time with all the same members
We need to generate LINQ queries which are 100% unknown during coding (design time).
I need to generate a set of coordinates in Erlang. Given one coordinate, say
I need to generate unique 64 bits integers from Python. I've checked out the

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.