Method application in Java random, Threadlocalrandom, UUID class (random number)

Source: Internet
Author: User
Tags uuid

1.random: Produces a pseudo-random number (by the same seed, the resulting random number is the same);

Random r=new random (); System.out.println (R.nextboolean ()); System.out.print (R.nextint (50) + ",");//random generation of 0~50 random number, excluding 50system.out.println (R.nextint (20) +30);//random generation of 30~50 random number, Not including 50

2.Threadlocalrandom: It is JDK 7 that provides concurrent generation of random numbers that can resolve competing battles that occur across multiple threads. Threadlocalrandom is not instantiated directly with new, but is used for the first time with its static method, current ().
Changes from Math.random () to Threadlocalrandom have the following benefits:
We no longer have the contention to access the same random number generator instance from multiple threads.
Instead of instantiating a random number generator instance for each of the previous random variables, we can instantiate one per thread.

Threadlocalrandom t=threadlocalrandom.current (); System.out.println (T.nextint (50));//random generation of 0~50 random number, excluding 50system.out.println (T.nextint (30, 50));//random generation of 30~50 random number, Not including 50

3.uuid: uuid meaning is a universal unique identification code (universally unique Identifier), which is a software construction standard, is also the Open Software Foundation, OSF A part of the organization in the field of distributed computing environments (distributed Computing Environment, DCE). The purpose of the UUID is to allow all elements in a distributed system to have unique identification information without the need to specify the information through the central control terminal. In this way, everyone can create a UUID that does not clash with others. In such a case, there is no need to consider the name duplication problem when the database is established. Currently the most widely used UUID is Microsoft's globally Unique Identifiers (GUIDs), while other important applications are the Linux ext2/ext3 file system, LUKS encryption partition, GNOME, KDE, Mac OS X, and so on.

String U=uuid.randomuuid (). toString (); SYSTEM.OUT.PRINTLN (U);

Generate a 5-character verification code:

String content= "Abcdefghijklmnopqrstuvwhyz"; Content+=content.tolowercase (); content+= "0123456789"; Random r=new random (); StringBuilder b=new StringBuilder (5); for (int i = 0; I <5; i++) {char N=content.charat (R.nextint (Content.length ())); B. Append (n);} System.out.println (B.tostring ());

Method application in Java random, Threadlocalrandom, UUID class (random number)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.