java生成八位纯英文的随机数
时间: 2023-10-21 21:14:20 浏览: 76
可以使用Java的Random类和StringBuilder类来生成八位纯英文的随机数。具体实现代码如下:
```java
import java.util.Random;
public class RandomStringGenerator {
public static void main(String[] args) {
String randomString = generateRandomString(8);
System.out.println("Random string: " + randomString);
}
public static String generateRandomString(int length) {
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
StringBuilder builder = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++) {
int index = random.nextInt(characters.length());
builder.append(characters.charAt(index));
}
return builder.toString();
}
}
```
运行结果:
```
Random string: XrTQrKZo
```
阅读全文