JAVA生成16位唯一字符串

 

 

import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.UUID;


public class RandomUtils {


    private static long getRandom(long n) {
        long min = 1, max = 9;
        for (int i = 1; i < n; i++) {
            min *= 10;
            max *= 10;
        }
        long rangeLong = (((long) (new Random().nextDouble() * (max - min)))) + min;
        return rangeLong;
    }


    /**
     * 生成16位唯一字符串
     * @return
     */
    public static String getUniqueId(){
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        uuid=uuid.substring(0,10);
        StringBuilder builder=new StringBuilder();
        builder.append(uuid)
                .append(getRandom(3));
        return builder.toString();
    }


}

 

posted @ 2023-11-10 13:57  yvioo  阅读(557)  评论(0编辑  收藏  举报