All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.dennisit.vplus.data.utils.RandomUtils Maven / Gradle / Ivy

There is a newer version: 2.0.8
Show newest version
/*--------------------------------------------------------------------------
 *  Copyright (c) 2010-2020, Elon.su All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the elon developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: Elon.su, you can also mail [email protected]
 *--------------------------------------------------------------------------
 */
package com.github.dennisit.vplus.data.utils;

import java.security.SecureRandom;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Created by Elon.su on 2018/1/28.
 */
public class RandomUtils {

    /**
     * 根据模板生成排序
     *
     * @param length  目标串长度
     * @param templet 目标模板长度
     * @return 结果串
     */
    public static String randomTemplet(int length, String templet) {
        if (length <= 0 || StringUtils.isBlank(templet)) {
            throw new IllegalArgumentException("参数错误");
        }
        StringBuilder sb = new StringBuilder(length);
        SecureRandom random = new SecureRandom();
        for (int i = 0; i < length; i++) {
            sb.append(templet.charAt(random.nextInt(templet.length())));
        }
        return sb.toString();
    }

    /**
     * 生成一个随机数
     *
     * @return 结果串
     */
    public static String randUUID() {
        return randUUID(1).get(0);
    }

    /**
     * 生成指定数量的随机数(UUID)
     *
     * @param num 随机数量
     * @return 随机数集合
     */
    public static List randUUID(int num) {
        num = num <= 0 ? 1 : num;
        return Stream.generate(UUID::randomUUID)
                .map(u -> u.toString())
                .limit(num).collect(Collectors.toList());
    }

    /**
     * 生成指定数量的随机数
     *
     * @param num   随机数数量
     * @param bound 区间范围
     * @return 随机数集合
     */
    public static List randNumber(int num, int bound) {
        Number[] array = new Number[num];
        Arrays.parallelSetAll(array, index -> ThreadLocalRandom.current().nextInt(bound));
        return Arrays.asList(array);
    }

    /**
     * 生成一个随机数
     *
     * @param bound 区间范围
     * @return 随机数
     */
    public static Number randNumber(int bound) {
        return randNumber(1, bound).get(0);
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy