基于用户id的最优邀请码生成方案

    在程序开发中,经常会遇到生成邀请码的需求,最近在开发海盗鼠的过程中,也遇到了邀请码生成的问题,Google了一把,没有发现好的生成方案,没办法,只能自己造轮子了,在这里把实现方案记录下来,方便大家,当然如果你有更好的实现方案也可以告诉我。

   实现了通过用户id直接生成6位不重复字符邀请码,通过邀请码直接计算用户id

    需求如下:

  1. 邀请码由6位不重复数字字母组成
  2. 用户id和邀请码可以相互转化

实现思路大概是

//验证码字符列表
private static final char STUFFS[] = { 	
	'A','B','C','D','E','F','G','H',
	'I','J','K','L','M','N','P','Q',
	'R','S','T','U','V','W','X','Y',
	'1','2','3','4','5','6','7','8'};

32个字母数字中取6个字符进行排列组合,共可生成32*31*30*29*28*27=652458240个邀请码,我们只需要实现652458240个数字和其一一对应即可,下面是编码的过程,解码过程逆向就可以了。

1、将数字拆分成组合序号和排列序号

 1:ABCDEF  2:ABCDEG 3:ABCDEH 以此类推

//PERMUTATION = 6!
//MAX_COMBINATION = 32!/(32-6)!/6!
public static String encode(int val) {
	int com = val / PERMUTATION;
	if(com >= MAX_COMBINATION) {
		throw new RuntimeException("id can't be greater than 652458239");
	}
	int per = val % PERMUTATION;
	char[] chars = combination(com);
	chars = permutation(chars,per);
	return new String(chars);
}

2、通过组合序号获取字符组合

private static char[] combination(int com){
	char[] chars = new char[LEN];
	int start = 0;
	int index = 0;
	while (index < LEN) {
		for(int s = start; s < STUFFS.length; ++s ) {
			int c = combination(STUFFS.length - s - 1, LEN - index - 1);
			if(com >= c) {
				com -= c;
				continue;
			}
			chars[index++] = STUFFS[s];
			start = s + 1;
			break;
		}
	}
	return chars;
}

3、通过排列序号对字符进行排序

private static char[] permutation(char[] chars,int per){
	char[] tmpchars = new char[chars.length];
	System.arraycopy(chars, 0, tmpchars, 0, chars.length);
	int[] offset = new int[chars.length];
	int step = chars.length;
	for(int i = chars.length -1;i >= 0;--i) {
		offset[i] = per % step;
		per /= step;
		step --;
	}
	for(int i = 0; i < chars.length;++i) {
		if(offset[i] == 0)
		    continue;
		char tmp = tmpchars[i];
		tmpchars[i] = tmpchars[i - offset[i]];
		tmpchars[i - offset[i]] = tmp;
	}
	return tmpchars;
}

3、测试用例

Random random = new Random();
for(int i = 0; i < 100000;++i) {
	int id = random.nextInt(652458240);
	String code = encode(id);
	int nid = decode(code);
	System.out.println(  id + " -> " + code + " -> " + nid);
}

结果如下:

521926735 -> 1SVR5G -> 521926735
281504940 -> CRKISU -> 281504940
174311333 -> WSQMFB -> 174311333
198381828 -> V3IBN6 -> 198381828
450572266 -> T1VHFJ -> 450572266
229325485 -> LJGC4D -> 229325485
132906750 -> CIVBW4 -> 132906750
388658714 -> FPX2EM -> 388658714
184756314 -> 2BGPT8 -> 184756314

想不到还有人关注,那我附上源码好了

public class CicadaUtils {
	//验证码长度
	private static final int LEN = 6;
	//验证码字符列表
	private static final char STUFFS[] = { 	
			'A','B','C','D','E','F','G','H',
			'I','J','K','L','M','N','P','Q',
			'R','S','T','U','V','W','X','Y',
			'1','2','3','4','5','6','7','8'};
	
	private static final int PERMUTATION;
	private static final int MAX_COMBINATION;
	
	static {
		PERMUTATION = permutation(LEN);
		MAX_COMBINATION = combination(STUFFS.length,LEN);
	}
	
	private static int combination(int n,int m) {
		int com = 1;
		for(int i = n - m + 1; i <= n ; ++i) {
			com *= i;
		}
		for(int i = 2; i <= m ; ++i ) {
			com /= i;
		}
		return com;
	}
	
	private static int permutation(int n) {
		int per = 1;
		for(int i = 2; i <= n ; ++i) {
			per *= i;
		}
		return per;
	}
	
	public static int decode(String code) {
		if(code.length() != LEN) {
			throw new RuntimeException("invalid code");
		}
		char[] chars = new char[LEN];
		for(int i = 0; i < LEN;++i) {
			chars[i] = code.charAt(i);
		}
		int com = combination(chars);
		int per = permutation(chars);
		return com * PERMUTATION + per;
	}
	
	public static String encode(int val) {
		int com = val / PERMUTATION;
		if(com >= MAX_COMBINATION) {
			throw new RuntimeException("id can't be greater than 652458239");
		}
		int per = val % PERMUTATION;
		char[] chars = combination(com);
		chars = permutation(chars,per);
		return new String(chars);
	}
	
	private static char[] combination(int com){
		char[] chars = new char[LEN];
		int start = 0;
		int index = 0;
		while (index < LEN) {
			for(int s = start; s < STUFFS.length; ++s ) {
				int c = combination(STUFFS.length - s - 1, LEN - index - 1);
				if(com >= c) {
					com -= c;
					continue;
				}
				chars[index++] = STUFFS[s];
				start = s + 1;
				break;
			}
		}
		return chars;
	}
	
	private static char[] sort(char[] src) {
		char[] sort = new char[src.length];
		int index = 0;
		for(int i = 0; i < STUFFS.length;++i) {
			if(find(src, STUFFS[i]) != -1) {
				sort[index++] = STUFFS[i];
			}
		}
		return sort;
	}
	
	private static int combination(char[] chars) {
		int[] offset = new int[LEN];
		char[] sort = sort(chars);
		for(int i = 0; i < sort.length;++i) {
			offset[i] = find(STUFFS, sort[i]);
			if(offset[i] == -1) {
				throw new RuntimeException("invalid code");
			}
		}
		int com = 0;
		for(int i = 0;i < offset.length;++i) {
			if(i == 0) {
				if(offset[0] == 0) {
					continue;
				}
				for(int n = 0; n < offset[0];++n) {
					com += combination(STUFFS.length -  n - 1, LEN - 1);
				}
				continue;
			}
			
			if(offset[i] - offset[i - 1] <= 1) {
				continue;
			}
			
			for(int n = offset[i-1] + 1; n < offset[i];++n) {
				com += combination(STUFFS.length -  n - 1, LEN - i - 1);
			}
		}
		
		return com;
	}
	
	private static char[] permutation(char[] chars,int per){
		char[] tmpchars = new char[chars.length];
		System.arraycopy(chars, 0, tmpchars, 0, chars.length);
		int[] offset = new int[chars.length];
		int step = chars.length;
		for(int i = chars.length -1;i >= 0;--i) {
			offset[i] = per % step;
			per /= step;
			step --;
		}
		
		for(int i = 0; i < chars.length;++i) {
			if(offset[i] == 0)
				continue;
			char tmp = tmpchars[i];
			tmpchars[i] = tmpchars[i - offset[i]];
			tmpchars[i - offset[i]] = tmp;
		}
		
		return tmpchars;
	}
	
	private static int find(char[] chars,char ch) {
		for(int i = 0;i < chars.length;++i) {
			if(chars[i] == ch) {
				return i;
			}
		}
		return -1;
	}
	
	private static int permutation(char[] chars){
		char[] sort = sort(chars);
		int[] offset = new int[chars.length];
		for(int i =chars.length -1;i >=0;--i ) {
			int f = find(chars, sort[i]);
			offset[i] = i - f;
			char tmp = chars[i];
			chars[i] = chars[i - offset[i]];
			chars[i - offset[i]] = tmp;
		}
		int per = 0;
		int step = 1;
		for(int i = 0; i < offset.length;++i ) {
			per = per * step + offset[i];
			step++;
		}
		return per;
	}
}

评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值