网上看到一个例子,借鉴修改一下

实现根据long类型的用户ID生成6位随机邀请码,并且根据邀请码能算出用户ID。代码如下:

    /** 自定义进制(选择你想要的进制数,不能重复且最好不要0、1这些容易混淆的字符) */
private static final 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 final char b='a'; /** 进制长度 */
private static final int binLen=r.length; /** 邀请码长度 */
private static final int s=6; /**
* 根据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(b);
Random rnd=new Random();
for(int i=1; i < s - str.length(); i++) {
sb.append(r[rnd.nextInt(binLen)]);
}
str+=sb.toString();
}
return str;
} /**
* 根据随机码生成ID
* @param 随机码
* @return ID
*/
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;
}

最新文章

  1. java nio(non-blocking io)简介及和io
  2. About SOuP
  3. All About Python
  4. Laravel Predis Error while reading line from the server.
  5. Windows 7 32位上硬盘安装linux[ubuntu13.04] 双系统
  6. Linux磁盘管理命令
  7. Oracle Data Guard
  8. kettle Row Normaliser(行转列)
  9. How to get Directory size in IsolatedStorage of Windows Phone 8 App
  10. hdoj 2085 核反应堆【水】
  11. 我是如何理解ThreadLocal
  12. webpack3中文版使用参考文档--全面解析webpack.config.js
  13. Nginx限流办法
  14. Web Service进阶(七)浅谈SOAP Webservice和RESTful Webservice
  15. Asp.Net Core WebAPI使用Swagger时API隐藏与分组
  16. Centos 6.9 install Python3.7
  17. Nginx:413 Request Entity Too Large
  18. java复制文件夹中的所有文件和文件夹到另一个文件夹中
  19. [转帖]你们知道LTE Cat.1到Cat.10 那Cat.0呢?
  20. spring cloud 服务提供者

热门文章

  1. 【转】值得推荐的C/C++框架和库
  2. Sleep 比对 (Win32API 与 STL )
  3. mvc 自定义 AuthorizeAttribute 验证逻辑
  4. Obama&amp;nbsp;unveils&amp;nbsp;his&amp;amp;nbsp…
  5. 使用军哥的lnmp配置虚拟主机,需要注意的是要配置hosts文件
  6. ADT-Bundle--Android开发环境快速搭建
  7. yzm10与战地信使 yzm10原创系列
  8. EF外键保存数据
  9. 无法加载MainifestResourceTransformer
  10. MyCat - 使用篇(5)