邀请码生成器(与ID互转化) 您所在的位置:网站首页 gamesense邀请码生成器 邀请码生成器(与ID互转化)

邀请码生成器(与ID互转化)

2023-10-16 05:38| 来源: 网络整理| 查看: 265

邀请码生成器,算法原理: 1、获取id: 例:1111111 2、使用自定义进制转为:例:gpm6 3、 转为字符串,并在后面加’O’字符:例:gpm6o 4、在后面随机产生若干个随机数字字符:例:gpm6o7 5、转为自定义进制后就不会出现o这个字符,然后在后面加个’o’,这样就能确定唯一性。最后在后面产生一些随机字符进行补全。

public class ShareCodeUtil { /** 自定义进制(0,1没有加入,容易与o,l混淆) */ private static final char[] r = new char[] { 'Q', 'W', 'E', '8', 'A', 'S', '2', 'D', 'Z', 'X', '9', 'C', '7', 'P', '5', 'K', '3', 'M', 'J', 'U', 'F', 'R', '4', 'V', 'Y', 'L', 'T', 'N', '6', 'B', 'G' }; /** (不能与自定义进制有重复) */ private static final char b = 'H'; /** * 进制长度 */ 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[30]; int charPos = 30; 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, (30 - 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; } 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; } public static void main(String[] args) throws IOException { for (int i = 0; i < 30; i++) { String ss="邀请码:" + toSerialCode(1); System.out.println(ss); System.out.println(codeToId(ss)); } }


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有