生成唯一不重复的随机数

  1. 在日常开发中需要通过程序生成一系列不重复的随机数,通过Java自带的类库也可以实现,但是在高并发情况下会出现重复的情况,为了尽可能解决不重复的问题,我们通过根据ip地址,MAC地址,访问时间以及哈希算法生成唯一不重复的唯一编号以及访问IP地址获取
  2. 根据UUID全球唯一标识符也可以实现不重复的编号,具体代码如下
  3.   1 package com.sun.generat;
      2 
      3 import java.net.InetAddress;
      4 import java.util.Random;
      5 import java.util.UUID;
      6 
      7 /**
      8 * @ClassName: UUIDHexGenerator
      9 * @Description:生成不重复的随机数
     10 * @author sunt
     11 * @date 2017年8月17日 上午11:09:27
     12 */ 
     13 public class UUIDHexGenerator {
     14 
     15      private String sep = "";
     16         private static final int IP;
     17         static {
     18             int ipadd;
     19             try {
     20                 ipadd = toInt(InetAddress.getLocalHost().getAddress());
     21             } catch (Exception e) {
     22                 ipadd = 0;
     23             }
     24             IP = ipadd;
     25         }
     26         private static short counter = (short) 0;
     27         private static final int JVM = (int) (System.currentTimeMillis() >>> 8);
     28 
     29         public static int toInt(byte[] bytes) {
     30             int result = 0;
     31             for (int i = 0; i < 4; i++) {
     32                 result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];
     33             }
     34             return result;
     35         }
     36 
     37         /**
     38          * Unique in a local network
     39          */
     40         protected int getIP() {
     41             return IP;
     42         }
     43 
     44         /**
     45          * Unique down to millisecond
     46          */
     47         protected short getHiTime() {
     48             return (short) (System.currentTimeMillis() >>> 32);
     49         }
     50 
     51         protected int getLoTime() {
     52             return (int) System.currentTimeMillis();
     53         }
     54 
     55         /**
     56          * Unique across JVMs on this machine (unless they load this class in the
     57          * same quater second - very unlikely)
     58          */
     59         protected int getJVM() {
     60             return JVM;
     61         }
     62 
     63         protected String format(int intval) {
     64             String formatted = Integer.toHexString(intval);
     65             StringBuffer buf = new StringBuffer("00000000");
     66             buf.replace(8 - formatted.length(), 8, formatted);
     67             return buf.toString();
     68         }
     69 
     70         protected String format(short shortval) {
     71             String formatted = Integer.toHexString(shortval);
     72             StringBuffer buf = new StringBuffer("0000");
     73             buf.replace(4 - formatted.length(), 4, formatted);
     74             return buf.toString();
     75         }
     76 
     77         public String generate() {
     78             return new StringBuilder(36).append(format(getIP())).append(sep)
     79                     .append(format(getJVM())).append(sep)
     80                     .append(format(getHiTime())).append(sep)
     81                     .append(format(getLoTime())).append(sep)
     82                     .append(format(getCount())).toString();
     83         }
     84 
     85         /**
     86          * Unique in a millisecond for this JVM instance (unless there are >
     87          * Short.MAX_VALUE instances created in a millisecond)
     88          */
     89         protected short getCount() {
     90             synchronized (UUIDHexGenerator.class) {
     91                 if (counter < 0)
     92                     counter = 0;
     93                 return counter++;
     94             }
     95         }
     96 
     97         public static void main(String[] args) {
     98             UUIDHexGenerator uuidHexGenerator = new UUIDHexGenerator();
     99             String generate = uuidHexGenerator.generate();
    100             System.out.println("---------generate:" + generate);
    101             int num = uuidHexGenerator.getIP();
    102             System.out.println("生成的随机数:" + num);
    103             System.out.println(UUID.randomUUID().toString());
    104             System.out.println(UUID.randomUUID().toString().replaceAll("\\-", ""));
    105             
    106         }
    107 }

     

posted @ 2017-08-17 11:22  sunny1009  阅读(728)  评论(0编辑  收藏  举报