随机产生字母a--z, A-Z 的任意组合
VERSION 1.0 引自: http://www.coderanch.com/t/134491/Security/generating-secure-tokens
package demo;
import java.util.Random; /* * This code is a discussion of an opinion in a technical forum. * You may develope ideas from this code. You may not use it directly. */ public class DM{ String getRandomIdentifier(int length) throws Exception { final int limitingValue = 64;// Whatever you decide, not me. if(length < limitingValue) { try { // The first step is to get a filename generator char lowerBound = 'a'; char upperBound = 'z'; Random randomCharacterGenerator = new Random(); // Then get some characters char[] identifierBuffer = new char[length];// int index = identifierBuffer.length;// final int numericLowerBound = (int) lowerBound; final int numericUpperBound = (int) upperBound; final int range = numericUpperBound - numericLowerBound;// do { // recoded in mesage edit, original defective int getOne = randomCharacterGenerator.nextInt(range); int next = numericLowerBound + getOne; identifierBuffer[--index] = (char) next; } while(index > 0x00000000); return new String(identifierBuffer);// } catch(ArrayIndexOutOfBoundsException aioobe) { System.out.println(aioobe.getMessage()); } } else { throw new Exception("Contact system administrator.");// } return null; }}VERSION 2.0 改进型:
package token;
import java.util.Random;
public class DM {
public static void main(String[] args) throws Exception {
System.out.println("1==97===:" + (int) 'a');
System.out.println("2==122===:" + (int) 'z');
System.out.println("3==65===:" + (int) 'A');
System.out.println("4===90==:" + (int) 'Z');
DM mn = new DM();
System.out.println(mn.getRandomIdentifier(26));
}
String getRandomIdentifier(int length) throws Exception {
final int limitingValue = 164;// Whatever you decide, not me.
if (length < limitingValue) {
try {
// 26个小写+26个大小 = 52 字母
final int range = 52;//
char[] charStr = new char[range];
int j = 0;
// A=65, z =122
for (int i = 65; i <= 122; i++) {
// Z--a 之间的跳过
if (i > 90 && i < 97) {
continue;
}
charStr[j] = (char) i; // 这里将保存52个大小字母
j++;
}
// 这里其实可以将 0 - 9 的数字也添加进去
Random randomCharacterGenerator = new Random();
// Then get some characters
char[] identifierBuffer = new char[length];//
int index = identifierBuffer.length;//
do {
// 产生0至51 共52个随机数,用于索引字母数组
int getOne = randomCharacterGenerator.nextInt(range);
identifierBuffer[--index] = charStr[getOne];
} while (index > 0x00000000);
return new String(identifierBuffer);//
} catch (ArrayIndexOutOfBoundsException aioobe) {
System.out.println(aioobe.getMessage());
}
} else {
throw new Exception("Contact system administrator.");//
}
return null;
}
}
随机产生字母a--z, A-Z 的任意组合的更多相关文章
- 用js正则判断输入的两位字符,第一位是数字16进制的,第二位是I、O、Q、U除外的字母(A到Z)
项目中遇到客户的需求变化,要验证某个数据的正确性,判断输入的两位字符,第一位是数字16进制的,第二位是I.O.Q.U除外的字母(A到Z). 本来对js不熟练,网上参考了一下js正则验证的规则,使用正则 ...
- ruby中的\z与\Z区别
s = "this is\nthe name\n" puts "--------------" puts s.match(/name\Z/) puts s.ma ...
- hdu4282 x^z+y^z+x*y*z=k 解的个数
题意: x^z + y^z + x*y*z = k; (x < y ,z > 1),给你一个k问有多少组解. 思路: 暴力枚举z,y,然后二分查找x.注意一点最好用 ...
- js随机生成字母数字组合的字符串 随机动画数字
效果描述: 附件中只有一个index.html文件有效 其中包含css以及html两部分内容 纯js生成的几个随机数字 每次都不重复,点击按钮后再次切换 使用方法: 1.将css样式引入到你的网页中 ...
- 【BZOJ】2038: [2009国家集训队]小Z的袜子(hose)(组合计数+概率+莫队算法+分块)
http://www.lydsy.com/JudgeOnline/problem.php?id=2038 学了下莫队,挺神的orzzzz 首先推公式的话很简单吧... 看的题解是从http://for ...
- QTP生成随机数字+字母
以下函数实现随机生成17位数(包括字母和数字),仍有改进的空间,可根据具体要求适当修改 Dim targetstring '调用返回函数给变量.Function过程通过函数名返回一个值 targets ...
- [Mathematics][Fundamentals of Complex Analysis][Small Trick] The Trick on drawing the picture of sin(z), for z in Complex Plane
Exercises 3.2 21. (a). For $\omega = sinz$, what is the image of the semi-infinite strip $S_1 = \{x+ ...
- 教你一招:EXCEL单元格随机生成字母
=CHAR(RANDBETWEEN(1,4)+65) 65代表大写字母A,依次类推 1代表从A开始 4代表到D结束
- php 随机显示据今天30天内的任意一天
function randomDate() { //echo date( "Y-m-d H:m:s", $newtime); //echo date("Y-m-d H:m ...
随机推荐
- 关于SWT常用组件(按钮,复选框,单选框(Button类))
Button是SWT中最常用的组件.Button类的继承关系图: Button类的构造方法是newe Button(Composite parent,int style)它有两个参数: 第一个参数:是 ...
- 理解JavaScript原型式继承
0.基础 javascript没有类的概念, javascript不需要实例化某个具体类的实例.javascript对象本身可以用来创建对象, 而对象可以继承自其他对象, 这个概念称为原型式继承 每个 ...
- Javascript中函数调用和this的关系
例子先行: var myObject={ foo:"bar", func:function(){ var self=this; console.log("outerfun ...
- 对象创建型模式------Builder(生成器)
本文系转载,转载地址http://blog.sina.com.cn/s/blog_59b6af690100zj3l.html,只供自己学习使用. 假设现在有三种产品,是玩具,都是由三部分组成,头,身体 ...
- openvswitch安装和使用 --修订通用教程的一些错误
1.下载openvswitch源文件,注意版本要适合操作系统内核. 推荐openvswitch2.0及其以上版本. 2.开始安装openvswitch cd openvswitch sudo ./bo ...
- ###STL学习--vector
点击查看Evernote原文. #@author: gr #@date: 2014-08-11 #@email: forgerui@gmail.com vector的相关问题.<stl学习> ...
- 关于Masonry框架(AutoLayout)的用法--面向初学者
Masonry作为目前较为流行的自动布局第三方框架,简单易用,大大减少了程序员花在UI布局和屏幕适配的精力与时间. 1 基本用法 1.1 事例1: 图1-1 // 首先是view1自动布局 [view ...
- 转:EF调用存储过程、函数
EF调用存储过程.函数 2014-04-02 09:12:20| 分类: ORM框架|举报|字号 订阅 一.ef4.1 codeFirst 修改表结构 增加字段等 EF code ...
- JavaScript基础-对象<1>
1.JavaScript内部对象属性和方法 (1)内置String对象 String 对象是JavaScript的核心对象之一. 创建一个sting对象: var a="this defin ...
- dubbo监控活跃线程数
telnet对应dubbo服务的ip+端口号 status -l 其中的active就是当前的活跃线程数 通过程序定时探测写入DB,再查询渲染出来就好了 监控报警,如果已经有监控平台,可以通过一定的规 ...