Java常用类学习:Rundom类和UUID

Java常用类学习:Rundom类和UUID

  • 生成随机数:

    • 在Java中,通常使用Math.random()或者Random类来获得随机数;

    •  

  • 代码案例:


    /**
    * Random类:生成随机数
    *
    *
    */
    public class RandomDemo01 {

       public static void main(String[] args) {

           Random random=new Random();
           System.out.println(random.hashCode());//

           int i=random.nextInt();
           System.out.println(i);//2095820655

           double b=random.nextDouble();
           System.out.println(b);//0.9803179250903287

           float f=random.nextFloat();
           System.out.println(f);//0.23888469

           long l=random.nextLong();
           System.out.println(l);//-7290130819150049334

      }
    }

     

  • UUID:

    • UUID:全局唯一标识符;它保证对在同一时空中的所有机器都是唯一的;

      public class RandomDemo01 {

         public static void main(String[] args) {
             //Random 和 UUID

             System.out.println(UUID.randomUUID());
             //ede791f9-36fb-4b21-b2e7-3b9cc22e751c

        }
      }

       
posted @ 2022-03-09 12:50  gzs1024  阅读(84)  评论(0)    收藏  举报