如何在ASP.NET中使用C#生成N个6位唯一键(无重复) [英] How to generates N no of 6 digit unique key (no duplicate) using C# in ASP.NET

查看:96
本文介绍了如何在ASP.NET中使用C#生成N个6位唯一键(无重复)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hii,

我想生成N个6位数的唯一字符串(字母数字)我试过下面的方法但得到这么多的重复值,请任何人都可以帮助我????



我的尝试:



  public   static   string  GetUniqueKey( int  maxSize)
{
char [] chars = new char [ 62 ];
chars =
abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 .ToCharArray();
byte [] data = new byte [ 1 ];
使用(RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider())
{
crypto.GetNonZeroBytes(data);
data = new byte [maxSize];
crypto.GetNonZeroBytes(data);
}
StringBuilder result = new StringBuilder(maxSize);
foreach byte b in 数据)
{
result.Append(chars [b%(chars.Length)]);
}
return result.ToString();
}

按钮点击----
int i = 0 ; i < LEN; i ++)
{

dt_Output.Rows.Add(GetUniqueKey( 6 ));


}

System.Data.DataSet ds = new System.Data.DataSet() ;
ds.Tables.Add(dt_Output);

解决方案

有两种方法可以生成有保证的唯一键:

1)递增,因此密钥(N + 1)是一个大于密钥(N)的固定数量 - 通常是一个。

2)通过检查所有当前生成的值并检查您没有重复。



第一个选项需要维护(通常)线程安全)下一个值计数器并立即递增它。

第二个意味着保留一个已发布数字列表,并在每次尝试分配一个新数字时进行检查以确保它还不存在。



随机数不能保证是唯一的 - 即使GUID不是,它只是它们被绘制的相空间是这样的非常不可能在正常的生命周期内重复它。


Quote:

如何生成N个6位唯一密钥(无重复)



您需要一个2步骤程序

- 您需要管理一个具有任务给出独特的价值观。这是在很长一段时间内确保众多独特密钥的唯一实用方法。

- 然后你需要一个能够在62基础编码该值的函数。

base962函数可以类似于:

  public   static   string  ToBase62( int 值, int 数字)
{
char [] chars = new char [ 62 ];
chars = abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 .ToCharArray();

if (Digits == 0 返回 ;
return ToBase62(Value /(chars.Length),Digits-1).Append(chars [Value%(chars.Length)]);
}


OrginalGriff已经很好地回答了你的问题。



你将拥有使用HashTable或ArrayList或List< string>。生成什么,检查List< string>中是否存在它在添加之前。继续这样做,直到List< string>的长度为止。匹配你需要的数量。



你必须将for()更改为while(),对于更大的maxSize可能还需要一段时间。

Hii,
I want to generate N no of 6 digit unique string(alpha numeric) i have tried below method but getting so many duplicates value please anyone can help me ????

What I have tried:

public static string GetUniqueKey(int maxSize)
        {
            char[] chars = new char[62];
            chars =
            "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
            byte[] data = new byte[1];
            using (RNGCryptoServiceProvider crypto = new RNGCryptoServiceProvider())
            {
                crypto.GetNonZeroBytes(data);
                data = new byte[maxSize];
                crypto.GetNonZeroBytes(data);
            }
            StringBuilder result = new StringBuilder(maxSize);
            foreach (byte b in data)
            {
                result.Append(chars[b % (chars.Length)]);
            }
            return result.ToString();
        }

On Button Click ----
   for (int i = 0; i < LEN; i++)
             {
                
                 dt_Output.Rows.Add(GetUniqueKey(6));

                
             }

             System.Data.DataSet ds = new System.Data.DataSet();
             ds.Tables.Add(dt_Output);

解决方案

There are two ways to generate guaranteed unique keys:
1) Incrementally, so that key(N + 1) is a fixed amount greater than key(N) - usually one.
2) By reviewing all the currently generated values and checking that you didn't repeat.

The first option entails maintaining a (normally thread safe) "next value" counter and incrementing it immediately it is used.
The second means keeping a list of "issued" numbers and checking each time you try to allocate a new one to make sure it doesn't exist yet.

Random numbers are not guaranteed to be unique - even GUIDs aren't, it's just that the phase space from which they are drawn is so huge as to make it phenomenally unlikely that you would repeat it within a normal lifetime.


Quote:

How to generates N no of 6 digit unique key (no duplicate)


You need a 2 steps procedure
- you need to manage a counter which have the task to give unique values. It is the only practical way to go ensure numerous unique keys over a long periods of time.
- Then you need a function that will encode that value in base 62.
The base962 function can look something like:

public static string ToBase62(int Value, int Digits)
{
    char[] chars = new char[62];
    chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890".ToCharArray();
    
    if (Digits==0) return"";
    return ToBase62(Value/(chars.Length), Digits-1).Append(chars[Value % (chars.Length)]);
}


OrginalGriff has answered your question very well.

You will have to use a HashTable or an ArrayList or a List<string>. What ever is generated, check if it exists in the List<string> before adding it. Keep doing this until the length of the List<string> matches your required count.

You will have to change the for() to a while(), and it might take a while too for larger maxSize.


这篇关于如何在ASP.NET中使用C#生成N个6位唯一键(无重复)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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