RandomUtil

Description

RandomUtil mainly provides a wrapper for the Random object in the JDK. Strictly speaking, the random numbers generated by Java are pseudo-random numbers, so the random results generated by Hutool’s wrapper are also pseudo-random. However, this kind of random result is sufficient for most cases.

Usage

  • RandomUtil.randomInt to get a random integer within a specified range

For example, if we want to generate a random number between [10, 100), we can use:

int c = RandomUtil.randomInt(10, 100);
  • RandomUtil.randomBytes to generate random bytes, usually used for password or salt generation
byte[] c = RandomUtil.randomBytes(10);
  • RandomUtil.randomEle to randomly get an element from a list
  • RandomUtil.randomEleSet to randomly get a certain number of non-duplicate elements from a list, and return a Set
Set<Integer> set = RandomUtil.randomEleSet(CollUtil.newArrayList(1, 2, 3, 4, 5, 6), 2);
  • RandomUtil.randomString to get a random string (containing only digits and characters)
  • RandomUtil.randomNumbers to get a string containing only digits
  • RandomUtil.weightRandom weighted random generator. Pass in objects with weights, and then randomly get objects according to the weights