import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
public class RandomAndThreadLocalRandomAndUUIDDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
Random random = new Random();
System.out.println(random.nextInt(50));
ThreadLocalRandom tlRandom = ThreadLocalRandom.current();
System.out.println(tlRandom.nextInt(20, 30));
//UUID一般用来表示一个随机的唯一的一个字符串
String uuid = UUID.randomUUID().toString();
System.out.println(uuid);
//生成一个五位数的随机字符串
//1、用UUID截取前五位实现
String code = UUID.randomUUID().toString().substring(0,5);
System.out.println(code);
System.out.println("-------------------------------");
//2、随机取出字符串中五位数实现
String str = "ABCDEFGHIJKLMNOOPQRSTUVWXYZ";
str += str.toLowerCase();
str += "0123456789";
StringBuilder sb = new StringBuilder(5);
for(int i = 0;i<5;i++)
{
char ch = str.charAt(new Random().nextInt(str.length()));
sb.append(ch);
}
System.out.println(sb);
}
}
15
26
f6b151ae-2961-446d-a949-6815013a6192
d46e8
-------------------------------
iXsAe