```java
```java import java.util.Random;
/* * This class provides utility methods for generating random data. * * @author Your Name * @version 1.0 / public class RandomDataGenerator {
/**
* The default random number generator.
*/
private static final Random random = new Random();
/**
* Generates a random integer between the given minimum and maximum values (inclusive).
*
* @param min The minimum value (inclusive).
* @param max The maximum value (inclusive).
* @return A random integer between the given minimum and maximum values.
*/
public static int generateRandomInt(int min, int max) {
return random.nextInt(max - min + 1) + min;
}
/**
* Generates a random double value between 0.0 (inclusive) and 1.0 (exclusive).
*
* @return A random double value between 0.0 and 1.0.
*/
public static double generateRandomDouble() {
return random.nextDouble();
}
/**
* Generates a random string of the specified length.
*
* @param length The desired length of the string.
* @return A random string of the specified length.
*/
public static String generateRandomString(int length) {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < length; i++) {
builder.append((char) generateRandomInt('a', 'z'));
}
return builder.toString();
}
/**
* Generates a random boolean value.
*
* @return A random boolean value.
*/
public static boolean generateRandomBoolean() {
return random.nextBoolean();
}
/**
* Generates a random email address.
*
* @return A random email address.
*/
public static String generateRandomEmail() {
return generateRandomString(10) + "@" + generateRandomString(5) + ".com";
}
/**
* Generates a random phone number.
*
* @return A random phone number.
*/
public static String generateRandomPhoneNumber() {
return String.format("(%d)%d-%d",
generateRandomInt(2, 9),
generateRandomInt(100, 999),
generateRandomInt(1000, 9999));
}
} ```
Markdown 格式的 Java 文档:
```markdown
RandomDataGenerator
This class provides utility methods for generating random data.
Author
Your Name
Version
1.0
Methods
generateRandomInt(int min, int max)
Generates a random integer between the given minimum and maximum values (inclusive).
Parameters:
- min: The minimum value (inclusive).
- max: The maximum value (inclusive).
Returns: A random integer between the given minimum and maximum values.
generateRandomDouble()
Generates a random double value between 0.0 (inclusive) and 1.0 (exclusive).
Returns: A random double value between 0.0 and 1.0.
generateRandomString(int length)
Generates a random string of the specified length.
Parameters:
- length: The desired length of the string.
Returns: A random string of the specified length.
generateRandomBoolean()
Generates a random boolean value.
Returns: A random boolean value.
generateRandomEmail()
Generates a random email address.
Returns: A random email address.
generateRandomPhoneNumber()
Generates a random phone number.
Returns: A random phone number. ```
说明:
- 该代码提供了生成随机整数、双精度浮点数、字符串、布尔值、电子邮件地址和电话号码的方法。
- 该文档使用 Markdown 格式编写,以便于阅读和理解。
- 每个方法都有详细的描述,包括参数、返回值和示例。
- 您可以根据需要修改代码和文档内容。
- 本文标签: 运维
- 本文链接: https://blog.sandy1029.cloud/article/42
- 版权声明: 本文由nisan原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权