用java生成随机数,要包含大小写字母和数字
时间: 2023-04-08 15:02:47 浏览: 169
可以使用Java中的Random类和StringBuilder类来生成随机数。以下是一个示例代码:
```
import java.util.Random;
public class RandomGenerator {
public static void main(String[] args) {
int length = 10; // 生成的随机数长度
String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; // 可选字符集
StringBuilder sb = new StringBuilder();
Random random = new Random();
for (int i = 0; i < length; i++) {
int index = random.nextInt(characters.length());
sb.append(characters.charAt(index));
}
String randomString = sb.toString();
System.out.println(randomString);
}
}
```
该代码将生成一个包含大小写字母和数字的长度为10的随机字符串。您可以根据需要更改长度和字符集。
相关问题
java生成随机数,要包含大小写字母和数字,保存在数组中
可以使用Java的Random类和StringBuilder类来生成随机数,并将其保存在数组中。以下是一个示例代码:
```
import java.util.Random;
public class RandomGenerator {
public static void main(String[] args) {
int length = 10; // 数组长度
char[] chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray(); // 可选字符
Random random = new Random();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
char c = chars[random.nextInt(chars.length)];
sb.append(c);
}
String randomString = sb.toString();
char[] randomChars = randomString.toCharArray();
System.out.println("随机生成的字符串为:" + randomString);
System.out.println("保存在数组中的随机字符为:");
for (int i = 0; i < randomChars.length; i++) {
System.out.print(randomChars[i] + " ");
}
}
}
```
运行结果:
```
随机生成的字符串为:qJL5t0zKwR
保存在数组中的随机字符为:
q J L 5 t 0 z K w R
```
java 怎么生成8位同时存在大小写字母和数字的随机密码
你可以使用以下代码生成8位同时存在大小写字母和数字的随机密码:
```java
import java.util.Random;
public class PasswordGenerator {
private static final String CHAR_LOWER = "abcdefghijklmnopqrstuvwxyz";
private static final String CHAR_UPPER = CHAR_LOWER.toUpperCase();
private static final String NUMBER = "0123456789";
private static final String DATA_FOR_RANDOM_STRING = CHAR_LOWER + CHAR_UPPER + NUMBER;
private static Random random = new Random();
public static void main(String[] args) {
int passwordLength = 8;
String password = generateRandomPassword(passwordLength);
System.out.println("Random Password: " + password);
}
private static String generateRandomPassword(int length) {
if (length < 1) {
throw new IllegalArgumentException("Password length must be at least 1.");
}
StringBuilder sb = new StringBuilder(length);
// Generate random characters
for (int i = 0; i < length; i++) {
int randomIndex = random.nextInt(DATA_FOR_RANDOM_STRING.length());
char randomChar = DATA_FOR_RANDOM_STRING.charAt(randomIndex);
sb.append(randomChar);
}
return sb.toString();
}
}
```
这个代码使用了一个包含了小写字母、大写字母和数字的字符串常量,然后使用 `Random` 类生成随机数来从字符串中选取字符,最终生成一个随机密码。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)