package org.springblade.imports.util;

import com.github.xiaoymin.knife4j.core.util.StrUtil;

import java.util.concurrent.ThreadLocalRandom;

public class RandomUtil {

	public static String randomString(int length) {
		return randomString("abcdefghijklmnopqrstuvwxyz0123456789", length);
	}

	public static String randomString(String baseString, int length) {
		if (StrUtil.isBlank(baseString)) {
			return "";
		} else {
			StringBuilder sb = new StringBuilder(length);
			if (length < 1) {
				length = 1;
			}

			int baseLength = baseString.length();

			for(int i = 0; i < length; ++i) {
				int number = randomInt(baseLength);
				sb.append(baseString.charAt(number));
			}

			return sb.toString();
		}
	}

	public static int randomInt(int limit) {
		return getRandom().nextInt(limit);
	}

	public static ThreadLocalRandom getRandom() {
		return ThreadLocalRandom.current();
	}
}
