要求:生成的这个身份码不能重复

直接上代码:

public static  String getCode(){
        Random random = new Random();
        //random.nextInt(2))该方法的作用是生成一个随机的int值,
        // 该值介于[0,n)的区间,也就是0到n之间的随机int值,包含0而不包含n。
        int  str=random.nextInt(2);
        System.out.println("产生的随机数是:"+str);
        //System.nanoTime()返回的是纳秒,
        // nanoTime而返回的可能是任意时间,甚至可能是负数
        long str2=System.nanoTime()+str;
        System.out.println("产生的随机数是+纳秒数返回是:"+str2);
        Long l = new Long(str2);
        System.out.println(l);
        //Long.toHexString(l)方法返回下列字符作为十六进制数字long参数以基数为16的无符号整数的
        String code=Long.toHexString(l).toUpperCase();
        return  code;
    }

    public static void main(String[] args) {
        System.out.println(getCode());
    }

这样生成的身份码在单机情况下不会是重复的。

 

相关文章:

  • 2022-01-24
  • 2021-11-30
  • 2022-01-14
  • 2021-06-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-02-18
  • 2022-12-23
  • 2022-12-23
  • 2021-12-05
  • 2022-01-06
  • 2021-10-04
相关资源
相似解决方案