在c#中生成7位唯一、随机和不可预测的字符串(包括0-9数字) [英] Generate 7 digit unique, random and unpredictable string (include 0-9 number) in c#

查看:53
本文介绍了在c#中生成7位唯一、随机和不可预测的字符串(包括0-9数字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为保存到我的数据库中的每条新记录生成 7 位唯一、随机和不可预测的字符串,包括 0-9 个数字,我发现 THIS 解决方案,但我仍然想知道如何确保该字符串在数据库中没有重复?

I want to generate 7 digit unique, random and unpredictable string include 0-9 number for each new record saved into my database, I found THIS solution but I'm still wonder about how can I be sure that the string are not duplicated in the database?

我应该将每个新的与保存在数据库中的所有其他人进行比较还是有更好的解决方案?

Should I compare each new one with the all others saved in database or there's a better solution?

如果我想为每个注册用户打印一张身份证,这张身份证包含7位随机数,我不希望这个数字是连续的,以避免每个人都知道注册了多少人,我也不知道'不希望数字是可预测的,

If I want to print an ID card for each registered user, This ID card contain the 7 digit random number, I don't want this number to be sequential to avoid every one know how much people that registered, Also I don't want the number to be predictable,

最好的例子是护照号码,它是唯一的、随机的和不可预测的包含数字和字符.我使用了 Guid 作为身份,但无法将其打印在 ID 卡上.

The best example is the passport number, It's unique, random and unpredictable contain numbers and characters. I used the Guid as an identity but I can't used it to print it on ID card.

推荐答案

嗯,一种方法是生成一个从 1 到 9999999 的 List,然后删除已经添加到数据库中的 itens.最后,使用从 0 到列表长度的随机数从中获取一个项目,将该项目添加到数据库并从列表中删除.像这样:

Well, one way to do it is generate a List from 1 to 9999999, then remove the itens already added to the database. Finally, use a random from 0 to the length of the list to get an item from it, add the item to database and remove from the list. Something like this:

//this is for the example, should be replaced by your database
var myDatabaseValues = new List<string>()
{
    "0000000",
    "9999999",
    "1234567"
};
//Put in a place where Random get initialized only once.
Random rdn = new Random();
//Generates all possibilites
var fullList = Enumerable.Range(0,10000000).ToList();
//Remove the itens already present in the DB.
foreach(var val in myDatabaseValues)
{
    int num = int.Parse(val);
    fullList.Remove(num);
}

//Then, to get a new number
var nextNum = rdn.Next(0,fullList.Count);
myDatabaseValues.Add(nextNum.ToString("D7"));
fullList.Remove(nextNum);

只要确保你是唯一一个连接数据库的人,或者每次都查询数据库.

Just make sure you are the only one wirting to the database, or query the DB evertytime.

根据您不希望它是可预测的评论,您不应该使用 Random 类,而是使用加密安全类.

Based on your comment that you don't want it to be predictable, you shouldn't use the Random class, but a cryptographic safe class.

根据你的新评论,我认为最好的方法是预先填充一个已经洗牌的所有可能性的表格,当你需要一个新的 id 时,你可以从表格中选择并删除它.像这样:

Based on your new comments, I think the best way will be pre-populate a table with all the possibilities already shuffled, and when you need a new id you pick it from the table and remove it. Something like this:

//Pre populate, should be done only once
//this is for the example, should be replaced by your database, ideally with a auto-increment first column so you can sort by it when you pick it.
var myDatabaseValues = new List<string>();

Random rdn = new Random();
//Generates all possibilites, and shufle it
var fullList = Enumerable.Range(0,10000000).OrderBy(x=>rdn.Next()).ToList();

foreach(var num in fullList){   
    myDatabaseValues.Add(num.ToString("D7"));   
}


//Now, when you need a item from db, pick one and remove it. 
//Just be sure to lock the table so multiple accesses don't read the same value at the same time
var nextVal = myDatabaseValues.First();
myDatabaseValues.Remove(nextVal);
//use the nextVal as you want

这篇关于在c#中生成7位唯一、随机和不可预测的字符串(包括0-9数字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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