67,513
社区成员
发帖
与我相关
我的任务
分享
public static void main(String[] args) {
int[] bits = new int[89999];
for (int i = 0; i < 89999; i++) {
bits[i] = 10000 + i;
}
for (int i = 0; i < 11110; i++) {
System.out.println(getOne(bits));
}
}
private static int getOne(int[] bits) {
int ret = -1;
for (int i = 0; i < 89999; i++) {
Random rd1 = new Random();
int rad = rd1.nextInt(89999);
if (bits[rad] != 0) {
ret = bits[rad];
bits[rad] = 0;
break;
} else {
System.out.println("collision -" + rad);
}
}
return ret;
}
10MOY8NW
01EFWB7L
0234UCRA
02QURE9Z
03GJPETO
0469OICD
16X0LIW2
05LQIKER
069EFLYG
1A06EOI5
18NWCP0U
08DM9RKJ
0E4D9T38
0BS15TLX
0CHR3W5M
0E7H1XPB
1EV6YZ90
1DJVY0RP
0HBMW2AE
1I0CT4U3
/** 最大值 */
public static final long MAX = 1220096908800L; // 36*35*34*33*32*31*30*29
/** 乘法用的素数 */
public static final long P = 982451653L;
/** 加法用的素数 */
public static final long Q = 9007;
/** 编码长度 */
public static final int LEN = 8;
/** 采用36进制 */
public static final int RADIX = 36;
/**
* 编码方法。
*
* @param number
* 序号
* @return 8位编码
* @throws IllegalArgumentException
* 如果序号超过范围
*/
public static String encode(long number) {
if (number <= 0 || number > MAX) {
throw new IllegalArgumentException();
}
long x = (number * P + Q) % MAX;
return encode0(x);
}
private static String encode0(long x) {
List<Character> list = new LinkedList<Character>();
for (int i = 0; i < RADIX; i++) {
list.add(Character.toUpperCase(Character.forDigit(i, RADIX)));
}
char[] codes = new char[LEN];
int radix = RADIX;
for (int i = LEN; i-- > 0;) {
int n = (int) (x % radix);
codes[i] = list.remove(n);
x /= radix;
radix--;
}
return new String(codes);
}
/** 最大值 */
public static final long MAX = 60466176; // 36 ^ 6
/** 乘法用的素数 */
public static final long P = 15485857;
/** 加法用的素数 */
public static final long Q = 9007;
/** 编码长度 */
public static final int LEN = 5;
/** 采用36进制 */
public static final int RADIX = 36;
/**
* 编码方法。
*
* @param number
* 序号
* @return 5位编码
* @throws IllegalArgumentException
* 如果序号超过范围
*/
public static String encode(int number) {
if (number <= 0 || number > MAX) {
throw new IllegalArgumentException();
}
long x = ((long) number * P + Q) % MAX;
char[] codes = new char[LEN];
Arrays.fill(codes, '0');
String str = Long.toString(x, RADIX);
System.arraycopy(str.toCharArray(),
0,
codes,
LEN - str.length(),
str.length());
reverse(codes);
return new String(codes).toUpperCase();
}
private static void reverse(char[] codes) {
for (int i = LEN >> 1; i-- > 0;) {
codes[i] ^= codes[LEN - i - 1];
codes[LEN - i - 1] ^= codes[i];
codes[i] ^= codes[LEN - i - 1];
}
}
此过程,“理论上”可逆,但是decode算法实际上没法用——太慢,几乎等同于把所有可能的值再去encode一遍做比较。
其中P越大,产生的编码越“随机”,但是请确保P*MAX小于Long.MAX_VALUE,且一定是素数。素数表可以去这里[/quote]
不好意思?现在券号生成规则变了,只需要8位随机码就可以了,要求每个字符在生成的8位码里不重复出现
WW389
LV0GI
AUXNR
ZSUV0
ORR3A
DQOBJ
2PLJS
RNIR1
GMFZA
5LC7K
UJ9FT
JI6N2
8H3VB
XF03L
MEXAU
BDUI3
0CRQC
PAOYL
E9L6V
38IE4