java使用uuid生成六位随机数
时间: 2024-05-23 20:06:23 浏览: 189
java实现随机生成UUID
以下是两种Java使用UUID生成六位随机数的例子:
1. 使用UUID生成六位随机数(只包含数字):
```java
import java.util.UUID;
public class RandomNumberGenerator {
public static void main(String[] args) {
String uuid = UUID.randomUUID().toString().replace("-", "");
String randomNumber = uuid.substring(0, 6);
System.out.println("Random number: " + randomNumber);
}
}
```
2. 使用UUID生成六位随机数(包含数字和字母):
```java
import java.util.UUID;
public class RandomNumberGenerator {
public static void main(String[] args) {
String[] chars = new String[] {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G"};
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 6; i++) {
int index = (int) (Math.random() * chars.length); sb.append(chars[index]);
}
String randomNumber = sb.toString();
System.out.println("Random number: " + randomNumber);
}
}
```
阅读全文