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)