or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

collection-operations.mdcompression-operations.mddate-time-operations.mdfile-io-operations.mdindex.mdobject-operations.mdrandom-operations.mdstring-operations.mdtype-conversion.mdurl-operations.mdxml-operations.md

random-operations.mddocs/

0

# Random Operations

1

2

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

3

4

## Capabilities

5

6

### Random Number Generation

7

8

Generate random numbers of various types with optional range constraints.

9

10

```java { .api }

11

/**

12

* Generate random boolean value

13

* @return true or false randomly

14

*/

15

public static boolean randomBoolean();

16

17

/**

18

* Generate random integer

19

* @return random int value

20

*/

21

public static int randomInt();

22

23

/**

24

* Generate random integer with upper limit

25

* @param limitExclude upper limit (exclusive)

26

* @return random int between 0 (inclusive) and limitExclude (exclusive)

27

*/

28

public static int randomInt(int limitExclude);

29

30

/**

31

* Generate random integer within range

32

* @param minInclude minimum value (inclusive)

33

* @param maxExclude maximum value (exclusive)

34

* @return random int within specified range

35

*/

36

public static int randomInt(int minInclude, int maxExclude);

37

38

/**

39

* Generate random long value

40

* @return random long value

41

*/

42

public static long randomLong();

43

44

/**

45

* Generate random long with upper limit

46

* @param limitExclude upper limit (exclusive)

47

* @return random long between 0 (inclusive) and limitExclude (exclusive)

48

*/

49

public static long randomLong(long limitExclude);

50

51

/**

52

* Generate random double value

53

* @return random double between 0.0 and 1.0

54

*/

55

public static double randomDouble();

56

57

/**

58

* Generate random double with upper limit

59

* @param limit upper limit (exclusive)

60

* @return random double between 0.0 and limit

61

*/

62

public static double randomDouble(double limit);

63

64

/**

65

* Generate random float value

66

* @return random float between 0.0 and 1.0

67

*/

68

public static float randomFloat();

69

```

70

71

**Usage Examples:**

72

73

```java

74

import cn.hutool.core.util.RandomUtil;

75

76

// Boolean generation

77

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

78

79

// Integer generation

80

int anyInt = RandomUtil.randomInt(); // any integer

81

int limitedInt = RandomUtil.randomInt(100); // 0-99

82

int rangeInt = RandomUtil.randomInt(10, 50); // 10-49

83

84

// Long generation

85

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

86

87

// Double generation

88

double percentage = RandomUtil.randomDouble(); // 0.0-1.0

89

double price = RandomUtil.randomDouble(100.0); // 0.0-100.0

90

```

91

92

### Random String Generation

93

94

Generate random strings with various character sets and constraints.

95

96

```java { .api }

97

/**

98

* Generate random string with letters and numbers

99

* @param length length of generated string

100

* @return random string

101

*/

102

public static String randomString(int length);

103

104

/**

105

* Generate random uppercase string

106

* @param length length of generated string

107

* @return random uppercase string

108

*/

109

public static String randomStringUpper(int length);

110

111

/**

112

* Generate random number string

113

* @param length length of generated string

114

* @return random numeric string

115

*/

116

public static String randomNumbers(int length);

117

118

/**

119

* Generate random string from base string

120

* @param baseString characters to choose from

121

* @param length length of generated string

122

* @return random string from base characters

123

*/

124

public static String randomString(String baseString, int length);

125

126

/**

127

* Generate random Chinese character

128

* @return random Chinese character

129

*/

130

public static char randomChinese();

131

132

/**

133

* Generate random numeric character

134

* @return random digit character (0-9)

135

*/

136

public static char randomNumber();

137

138

/**

139

* Generate random letter character

140

* @return random letter character (a-z)

141

*/

142

public static char randomChar();

143

```

144

145

**Usage Examples:**

146

147

```java

148

import cn.hutool.core.util.RandomUtil;

149

150

// String generation

151

String code = RandomUtil.randomString(8); // "aB3xY7pQ"

152

String upperCode = RandomUtil.randomStringUpper(6); // "MNPQRS"

153

String numericCode = RandomUtil.randomNumbers(4); // "1234"

154

155

// Custom character set

156

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

157

158

// Character generation

159

char chineseChar = RandomUtil.randomChinese(); // 随机中文字符

160

char digit = RandomUtil.randomNumber(); // '0'-'9'

161

char letter = RandomUtil.randomChar(); // 'a'-'z'

162

```

163

164

### Random Collection Operations

165

166

Select random elements from collections and arrays.

167

168

```java { .api }

169

/**

170

* Get random element from list

171

* @param list source list

172

* @return random element from list

173

*/

174

public static <T> T randomEle(List<T> list);

175

176

/**

177

* Get random element from array

178

* @param array source array

179

* @return random element from array

180

*/

181

public static <T> T randomEle(T[] array);

182

183

/**

184

* Get multiple random elements from list

185

* @param list source list

186

* @param count number of elements to select

187

* @return list of random elements

188

*/

189

public static <T> List<T> randomEles(List<T> list, int count);

190

191

/**

192

* Get random elements as new list

193

* @param source source list

194

* @param count number of elements to select

195

* @return new list with random elements

196

*/

197

public static <T> List<T> randomEleList(List<T> source, int count);

198

199

/**

200

* Get random elements as set

201

* @param collection source collection

202

* @param count number of unique elements to select

203

* @return set of random elements

204

*/

205

public static <T> Set<T> randomEleSet(Collection<T> collection, int count);

206

```

207

208

**Usage Examples:**

209

210

```java

211

import cn.hutool.core.util.RandomUtil;

212

import java.util.*;

213

214

// Random element selection

215

List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "David");

216

String randomName = RandomUtil.randomEle(names); // "Bob"

217

218

String[] colors = {"red", "green", "blue", "yellow"};

219

String randomColor = RandomUtil.randomEle(colors); // "green"

220

221

// Multiple element selection

222

List<String> randomNames = RandomUtil.randomEles(names, 2); // ["Alice", "David"]

223

Set<String> uniqueNames = RandomUtil.randomEleSet(names, 2); // {"Charlie", "Alice"}

224

```

225

226

### Random Generator Management

227

228

Access and configure random number generators.

229

230

```java { .api }

231

/**

232

* Get ThreadLocalRandom instance

233

* @return ThreadLocalRandom for current thread

234

*/

235

public static ThreadLocalRandom getRandom();

236

237

/**

238

* Create SecureRandom with custom seed

239

* @param seed custom random seed

240

* @return SecureRandom instance

241

*/

242

public static SecureRandom createSecureRandom(byte[] seed);

243

244

/**

245

* Get SHA1PRNG SecureRandom

246

* @return SecureRandom with SHA1PRNG algorithm

247

*/

248

public static SecureRandom getSecureRandom();

249

```

250

251

## Common Types

252

253

```java { .api }

254

// Random number generator interfaces

255

import java.util.concurrent.ThreadLocalRandom;

256

import java.security.SecureRandom;

257

import java.math.RoundingMode;

258

```