生成8位没有规则且不重复的防伪码,如果数据源List存在,则直接过滤原来List里面的数据,验证新生成的防伪码与原来的数据中有没有重复的码。
private List<string> CreateDataList(int number, List<string> list)
{
for (int i = 0; i < number; i++)
{
string code = CreateCode(8);//防伪码位数
if (!list.Contains(code))
{
list.Add(code);
}
else
{
i--;
}
}
return list;
}
private string CreateCode(int len)
{
string codeSerial = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z";//62
string[] arr = codeSerial.Split(',');
string code = "";
Random rand = new Random(unchecked((int)DateTime.Now.Ticks));
for (int i = 0; i < len; i++)
{
code += arr[rand.Next(0, arr.Length - 1)];
}
return code;
}