CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-cn-hutool--hutool-core

Comprehensive Java utility library providing collections, strings, beans, dates, I/O, and numerous other utility functions.

Pending
Overview
Eval results
Files

random-operations.mddocs/

Random Operations

Random number generation, string generation, and collection element selection utilities through the RandomUtil class.

Capabilities

Random Number Generation

Generate random numbers of various types with optional range constraints.

/**
 * Generate random boolean value
 * @return true or false randomly
 */
public static boolean randomBoolean();

/**
 * Generate random integer
 * @return random int value
 */
public static int randomInt();

/**
 * Generate random integer with upper limit
 * @param limitExclude upper limit (exclusive)
 * @return random int between 0 (inclusive) and limitExclude (exclusive)
 */
public static int randomInt(int limitExclude);

/**
 * Generate random integer within range
 * @param minInclude minimum value (inclusive)
 * @param maxExclude maximum value (exclusive)
 * @return random int within specified range
 */
public static int randomInt(int minInclude, int maxExclude);

/**
 * Generate random long value
 * @return random long value
 */
public static long randomLong();

/**
 * Generate random long with upper limit
 * @param limitExclude upper limit (exclusive)
 * @return random long between 0 (inclusive) and limitExclude (exclusive)
 */
public static long randomLong(long limitExclude);

/**
 * Generate random double value
 * @return random double between 0.0 and 1.0
 */
public static double randomDouble();

/**
 * Generate random double with upper limit
 * @param limit upper limit (exclusive)
 * @return random double between 0.0 and limit
 */
public static double randomDouble(double limit);

/**
 * Generate random float value
 * @return random float between 0.0 and 1.0
 */
public static float randomFloat();

Usage Examples:

import cn.hutool.core.util.RandomUtil;

// Boolean generation
boolean randomFlag = RandomUtil.randomBoolean(); // true or false

// Integer generation
int anyInt = RandomUtil.randomInt(); // any integer
int limitedInt = RandomUtil.randomInt(100); // 0-99
int rangeInt = RandomUtil.randomInt(10, 50); // 10-49

// Long generation  
long randomId = RandomUtil.randomLong(1000000, 9999999); // 7-digit ID

// Double generation
double percentage = RandomUtil.randomDouble(); // 0.0-1.0
double price = RandomUtil.randomDouble(100.0); // 0.0-100.0

Random String Generation

Generate random strings with various character sets and constraints.

/**
 * Generate random string with letters and numbers
 * @param length length of generated string
 * @return random string
 */
public static String randomString(int length);

/**
 * Generate random uppercase string
 * @param length length of generated string
 * @return random uppercase string
 */
public static String randomStringUpper(int length);

/**
 * Generate random number string
 * @param length length of generated string
 * @return random numeric string
 */
public static String randomNumbers(int length);

/**
 * Generate random string from base string
 * @param baseString characters to choose from
 * @param length length of generated string
 * @return random string from base characters
 */
public static String randomString(String baseString, int length);

/**
 * Generate random Chinese character
 * @return random Chinese character
 */
public static char randomChinese();

/**
 * Generate random numeric character
 * @return random digit character (0-9)
 */
public static char randomNumber();

/**
 * Generate random letter character
 * @return random letter character (a-z)
 */
public static char randomChar();

Usage Examples:

import cn.hutool.core.util.RandomUtil;

// String generation
String code = RandomUtil.randomString(8); // "aB3xY7pQ"
String upperCode = RandomUtil.randomStringUpper(6); // "MNPQRS"
String numericCode = RandomUtil.randomNumbers(4); // "1234"

// Custom character set
String hexString = RandomUtil.randomString("0123456789ABCDEF", 8); // "A3F7B2E1"

// Character generation
char chineseChar = RandomUtil.randomChinese(); // 随机中文字符
char digit = RandomUtil.randomNumber(); // '0'-'9'
char letter = RandomUtil.randomChar(); // 'a'-'z'

Random Collection Operations

Select random elements from collections and arrays.

/**
 * Get random element from list
 * @param list source list
 * @return random element from list
 */
public static <T> T randomEle(List<T> list);

/**
 * Get random element from array
 * @param array source array
 * @return random element from array
 */
public static <T> T randomEle(T[] array);

/**
 * Get multiple random elements from list
 * @param list source list
 * @param count number of elements to select
 * @return list of random elements
 */
public static <T> List<T> randomEles(List<T> list, int count);

/**
 * Get random elements as new list
 * @param source source list
 * @param count number of elements to select
 * @return new list with random elements
 */
public static <T> List<T> randomEleList(List<T> source, int count);

/**
 * Get random elements as set
 * @param collection source collection
 * @param count number of unique elements to select
 * @return set of random elements
 */
public static <T> Set<T> randomEleSet(Collection<T> collection, int count);

Usage Examples:

import cn.hutool.core.util.RandomUtil;
import java.util.*;

// Random element selection
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");
String randomName = RandomUtil.randomEle(names); // "Bob"

String[] colors = {"red", "green", "blue", "yellow"};
String randomColor = RandomUtil.randomEle(colors); // "green"

// Multiple element selection
List<String> randomNames = RandomUtil.randomEles(names, 2); // ["Alice", "David"]
Set<String> uniqueNames = RandomUtil.randomEleSet(names, 2); // {"Charlie", "Alice"}

Random Generator Management

Access and configure random number generators.

/**
 * Get ThreadLocalRandom instance
 * @return ThreadLocalRandom for current thread
 */
public static ThreadLocalRandom getRandom();

/**
 * Create SecureRandom with custom seed
 * @param seed custom random seed
 * @return SecureRandom instance
 */
public static SecureRandom createSecureRandom(byte[] seed);

/**
 * Get SHA1PRNG SecureRandom
 * @return SecureRandom with SHA1PRNG algorithm
 */
public static SecureRandom getSecureRandom();

Common Types

// Random number generator interfaces
import java.util.concurrent.ThreadLocalRandom;
import java.security.SecureRandom;
import java.math.RoundingMode;

Install with Tessl CLI

npx tessl i tessl/maven-cn-hutool--hutool-core

docs

collection-operations.md

compression-operations.md

date-time-operations.md

file-io-operations.md

index.md

object-operations.md

random-operations.md

string-operations.md

type-conversion.md

url-operations.md

xml-operations.md

tile.json