java生成随机不重复id

package net.jeeshop.core.util;
import java.util.Random;  
  
public class RandomId {  

    private Random random;  
    private String table;  
    public RandomId() {  
        random = new Random();  
        table = "0123456789";  
    }  
    public String randomId() {
    int id=random.nextInt(10);
        String ret = null,  
            num = String.format("%05d", id);  
        int key = random.nextInt(10),   
            seed = random.nextInt(100);  
        Caesar caesar = new Caesar(table, seed);
        num = caesar.encode(key, num);  
        ret = num   
            + String.format("%01d", key)   
            + String.format("%02d", seed);  
          
        return ret;  
    }  
    public static void main(String[] args) {  
        RandomId r = new RandomId();  
        for (int i = 0; i < 30; i += 1) {  
        }  
        System.out.println(r.randomId());  
        r.randomId();
    }  
    
    public class Caesar {  
        private String table;  
        private int seedA = 1103515245;  
        private int seedB = 12345;  
          
        public Caesar(String table, int seed) {  
            this.table = chaos(table, seed, table.length());  
        }  
        public Caesar(String table) {  
            this(table, 11);  
        }  
        public Caesar() {  
            this(11);  
        }  
        public Caesar(int seed) {  
            this("ABCDEFGHIJKLMNOPQRSTUVWXYZ", seed);  
        }  
        public char dict(int i, boolean reverse) {  
            int s = table.length(), index = reverse ? s - i : i;  
            return table.charAt(index);  
        }  
        public int dict(char c,  boolean reverse) {  
            int s = table.length(), index = table.indexOf(c);  
            return reverse ? s - index : index;  
        }  
        public int seed(int seed) {  
            long temp = seed;  
            return (int)((temp * seedA + seedB) & 0x7fffffffL);  
        }  
      
        public String chaos(String data, int seed, int cnt) {  
            StringBuffer buf = new StringBuffer(data);  
            char tmp; int a, b, r = data.length();  
            for (int i = 0; i < cnt; i += 1) {  
                seed = seed(seed); a = seed % r;  
                seed = seed(seed); b = seed % r;  
                tmp = buf.charAt(a);  
                buf.setCharAt(a, buf.charAt(b));  
                buf.setCharAt(b, tmp);  
            }  
            return buf.toString();  
        }  
      
        public String crypto(boolean reverse,  
                             int key, String text) {  
            String ret = null;  
            StringBuilder buf = new StringBuilder();  
            int m, s = table.length(), e = text.length();  
      
            for(int i = 0; i < e; i += 1) {  
                m = dict(text.charAt(i), reverse);  
                if (m < 0) break;  
                m = m + key + i;  
                buf.append(dict(m % s, reverse));  
            }  
            if (buf.length() == e)  
                ret = buf.toString();  
            return ret;  
        }  
        public String encode(int key, String text) {  
            return crypto(false, key, text);  
              
        }  
        public String decode(int key, String text) {  
            return crypto(true , key, text);  
        }  
          
//        public static void main(String[] args) {  
//            Caesar caesar = new Caesar();  
//            String data = caesar.encode(32, "APPLE");  
//            caesar.decode(32, data);  
//        }  
    }
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hughjin

动力动力动力动力动力动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值