import java.util.concurrent.ThreadLocalRandom;
/**
* Created by Kowalski on 2017/1/22.
*
*/
public class RandomIdWorker {
/**位运算数组*/
private static final int[] arr_32 = {
0x80000000,0x40000000,0x20000000,0x10000000,
0x08000000,0x04000000,0x02000000,0x01000000,
0x00800000,0x00400000,0x00200000,0x00100000,
0x00080000,0x00040000,0x00020000,0x00010000,
0x00008000,0x00004000,0x00002000,0x00001000,
0x00000800,0x00000400,0x00000200,0x00000100,
0x00000080,0x00000040,0x00000020,0x00000010,
0x00000008,0x00000004,0x00000002,0x00000001,
};
/**随机数最大值*/
private static int bitNum;
/**随机数长度*/
private static int circleLenNum;
/**当前圈数*/
private static int circleNum;
/**圈最大值*/
private static int maxCircleNum;
/**摧毁圈长度*/
private static int destoryLen;
/**环数组*/
private static int[] arr;
/**随机数*/
private static ThreadLocalRandom random = ThreadLocalRandom.current();
public static void init(int bitNum, int maxCircleNum, int destoryLen) {
if(destoryLen > bitNum) {
throw new IllegalArgumentException("destoryLen can not big than bitNum");
} else {
/**初始化数组*/
arr = new int[bitNum / 32 + 1];
/**初始化参数*/
RandomIdWorker.bitNum = bitNum;
RandomIdWorker.maxCircleNum = maxCircleNum;
RandomIdWorker.destoryLen = destoryLen;
for(circleLenNum = 10; bitNum / 10 != 0; bitNum /= 10) {
circleLenNum *= 10;
}
}
}
public static void main(String... args){
init(9999, 9999, 100);
int res = 0;
long time = System.currentTimeMillis();
for (int i = 0; i < 10000000; i++) {
res = generate();
}
System.out.println(System.currentTimeMillis() - time);
System.out.println(res);
System.out.println("圈数:" + circleNum);
}
public static synchronized int generate(){
/**取随机数*/
int ran = random.nextInt(bitNum);
/**核心代码*/
int i;
for(i = 0; i < destoryLen; ++i) {
if ((arr[ran / 32] & arr_32[ran % 32]) == 0) {
arr[ran / 32] |= arr_32[ran % 32];
return circleLenNum*circleNum + ran;
}
/**数已存在就取下一个*/
ran = (ran + 1) % bitNum;
}
/**到达最大圈数 从头开始*/
if(circleNum == maxCircleNum){
circleNum = 0;
}else {
++circleNum;
}
/**清空数组*/
for (i = 0; i < bitNum / 32 + 1; ++i) {
arr[i] = 0;
}
/**将新数组中ran位设成1*/
arr[ran / 32] |= arr_32[ran % 32];
return circleLenNum*circleNum + ran;
}
}
评论