java 随机字母数字生成算法

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);  
	}  
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值