生成随机唯一code [英] Generate Random Unique Code

查看:213
本文介绍了生成随机唯一code的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要生成一个九位数字code(无规preferably),这是唯一的一个给定的天(同一号不能在同一天再次产生)。我在考虑使用HHMMSSmmm(小时,分,秒,毫秒),生成唯一code,但不是真正随机的。这code生成方法可以通过多种方式在同一时间被访问,所以我将不得不把锁的方法。但将这种保证的数目是独一无二的,因为它有可能数产生可能比一毫秒采取以下,两个线程获得相同数目

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?

有没有更好的方法来生成一个随机的唯一的数字code这是唯一一个给定的一天吗?的位数可以是从6至9位。

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.

编辑:要产生的随机数的数量取决于交易的数量。最初的数目可以降低,但在一段时间可以变得非常高(多个事务每秒)。因此,我不喜欢数反对使用的列表比较,因为这可能会对性能问题。

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.

该随机数生成需要发生在一个ASP.NET MVC应用程序。

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

推荐答案

如果你从一个随机数与6位开始,然后不断增加随机的,但足够小的数字,你可能会被禁止时这样做。您可以使用文件系统作为锁定存储,如果你想......但我认为你应该使用一个数据库进行生产!

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!

下面是我所谈论的例子:

此示例是一个控制台应用程序,它使用一个文件来控制并发性,以及存储最近使用的号码和日期它生成的。

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!

这样可以为约999000的随机数,每天产生,在6到9位以下的范围内。这大约是每秒11号。

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.

这篇关于生成随机唯一code的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆