package com.lcs.code;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;
import org.junit.Test;
/**
* 随机码生成
* @author lcs
*
*/
public class RandomCodeUtil {
public static void main(String[] args) {
/*Map<String, Object> code = new ConcurrentHashMap<>();
long start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
code.put(randomStrAA(6), i);
}
long end = System.currentTimeMillis();
long time = end-start;
System.out.println("生成时间:"+time);
System.out.println(code.size());*/
for (int i = 1; i < 100; i++) {
//System.out.println(int_to_alphabet(i));
}
int_to_alphabet();
}
private static Random strGen = new Random();;
private static Random numGen = new Random();;
private static char[] numbersAndAA = ("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();;
private static char[] numbersAndAa = ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();;
private static char[] numbers = ("0123456789").toCharArray();;
@Test
public static void int_to_alphabet(){
try {
File fp=new File("D:\\a.txt");
Map<String, String> maps2 = randomStrAAMap(4, 1000000);
//System.setOut(new PrintStream(new FileOutputStream("system_out.txt")));
PrintWriter pfp;
pfp = new PrintWriter(fp);
pfp.print(maps2.keySet().toString());
pfp.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** * 产生N个随机字符串 * */
public static final Map<String,String> randomStrAAMap(int length,int N) {
Map<String, String> map = new ConcurrentHashMap<String, String>();
int i = 0;
while (map.size()<=N) {
i++;
System.out.println("执行第"+i+"次"+"集合大小"+map.size());
map.put(randomStrAA(length), i+"");
}
System.out.println("执行次数"+i);
return map;
}
/** * 产生随机字符串 * */
public static final String randomStrAA(int length) {
if (length < 1) {
return null;
}
char[] randBuffer = new char[length];
for (int i = 0; i < randBuffer.length; i++) {
randBuffer[i] = numbersAndAA[strGen.nextInt(35)];
}
return new String(randBuffer);
}
/** * 产生随机字符串 * */
public static final String randomStrAa(int length) {
if (length < 1) {
return null;
}
char[] randBuffer = new char[length];
for (int i = 0; i < randBuffer.length; i++) {
randBuffer[i] = numbersAndAa[strGen.nextInt(35)];
}
return new String(randBuffer);
}
/** * 产生随机数值字符串 * */
public static final String randomNumStr(int length) {
if (length < 1) {
return null;
}
char[] randBuffer = new char[length];
for (int i = 0; i < randBuffer.length; i++) {
randBuffer[i] = numbers[numGen.nextInt(9)];
}
return new String(randBuffer);
}
}
java 随机字母数字生成算法
最新推荐文章于 2024-05-31 10:33:54 发布