網上看到一個例子,源地址:https://www.aliyun.com/jiaocheng/536419.html 借鑒修改一下
實現根據long類型的用戶ID生成6位隨機邀請碼,並且根據邀請碼能算出用戶ID。代碼如下:
/// <summary>
/// 不重復隨機字符串類
/// </summary>
public class SerialNumberHelper
{
/** 自定義進制(選擇你想要的進制數,不能重復且最好不要0、1這些容易混淆的字符) */
private static readonly char[] r = new char[] { 'q', 'w', 'e', '8', 's', '2', 'd', 'z',
'x', '9', 'c', '7', 'p', '5', 'k', '3', 'm', 'j', 'u', 'f', 'r', '4', 'v', 'y', 't', 'n', '6', 'b', 'g', 'h' };
/** 定義一個字符用來補全邀請碼長度(該字符前面是計算出來的邀請碼,后面是用來補全用的) */
private static readonly char b = 'a';
/** 進制長度 */
private static readonly int binLen = r.Length;
/** 邀請碼長度 */
private static readonly int s = 6;
/// <summary>
/// 根據ID生成隨機碼
/// </summary>
/// <param name="id">ID</param>
/// <returns></returns>
public static String ToSerialCode(long id)
{
char[] buf = new char[32];
int charPos = 32;
while ((id / binLen) > 0)
{
int ind = (int)(id % binLen);
buf[--charPos] = r[ind];
id /= binLen;
}
buf[--charPos] = r[(int)(id % binLen)];
String str = new String(buf, charPos, (32 - charPos));
//不夠長度的自動隨機補全
if (str.Length < s)
{
StringBuilder sb = new StringBuilder();
sb.Append(b);
Random rnd = new Random();
for (int i = 1; i < s - str.Length; i++)
{
sb.Append(r[rnd.Next(binLen)]);
}
str += sb.ToString();
}
return str;
}
/// <summary>
/// 根據隨機碼生成ID
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public static long CodeToId(String code)
{
char[] chs = code.ToCharArray();
long res = 0L;
for (int i = 0; i < chs.Length; i++)
{
int ind = 0;
for (int j = 0; j < binLen; j++)
{
if (chs[i] == r[j])
{
ind = j;
break;
}
}
if (chs[i] == b)
{
break;
}
if (i > 0)
{
res = res * binLen + ind;
}
else
{
res = ind;
}
}
return res;
}
}
上面6位邀請碼能表示的最大ID為728999999(“hhhhhh”),729000000(“wqqqqqq”)就要進位了。
上面方法同一個id生成的邀請碼不唯一,如果想唯一則定義一個補位字符串就可以了:
// 補位字符串
private static final String e="atgsghj";
/*
* 根據ID生成六位隨機碼
* @param id ID
* @return 隨機碼
*/
public static String toSerialCode(long id) {
char[] buf=new char[32];
int charPos=32;
while((id / binLen) > 0) {
int ind=(int)(id % binLen);
buf[--charPos]=r[ind];
id /= binLen;
}
buf[--charPos]=r[(int)(id % binLen)];
String str=new String(buf, charPos, (32 - charPos));
// 不夠長度的自動補全
if(str.length() < s) {
StringBuilder sb=new StringBuilder();
sb.append(e.subSequence(0, s-str.length()));
str+=sb.toString();
}
return str;
}