随机产生字母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 ...
随机推荐
- 滚动新闻插件vticker
vTicker 是一款非常小巧的 jQuery 垂直滚动插件,压缩后只有 2KB.vTicker 支持自定义滚动时间.间隔时间.显示个数.滚动方向(向上/向下).容器高度等等,但它对 HTML 结构有 ...
- selendroid项目实战2--ruby下的TOAST定位
网上很多 python/java捕获toast的方法,但ruby的简直没见过. selendroid客户端是基于selenium,而不一定需要appium,所以很多selenium的方法可以直接使用, ...
- 滑动条slider
#include"ui/CocosGUI.h" using namespace ui; Text* displayValudLabel = Text::create("轻 ...
- Linux - 重定向与管道
标准输出重定向 ">" 操作符:覆盖目标文件内容 huey@huey-K42JE:~/huey/linux/cmdline$ date > foo huey@huey- ...
- 议:如何将树形菜单形式的数据转化成HTML的二维表(相同内容需合并单元格)
一般做OA类管理系统,经常涉及到“组织架构”的概念,那么像这种有上下层级关系的数据一般会做成树形菜单的方式显示,底层代码必定会用到递归算法.这篇随笔的目的就是要谈谈除了用树形菜单来显示这种上下层级关系 ...
- Asp.net MVC 4 视图相关和其他
@{ Layout = “…”} To define layout page Equivalent to asp.net master-page 要定义相当于ASP.NET母版页的页面布局 @mode ...
- HW--字符串加解密
package t0817; import java.util.Scanner; public class StringEncrypt { public static void main(String ...
- NSdata 与 NSString,Byte数组,UIImage 的相互转换
1. NSData 与 NSString NSData-> NSString NSString *aString = [[NSString alloc] initWithData:adataen ...
- [PR & ML 3] [Introduction] Probability Theory
虽然学过Machine Learning和Probability今天看着一part的时候还是感觉挺有趣,听惊呆的,尤其是Bayesian Approach.奇怪发中文的笔记就很多人看,英文就没有了,其 ...
- 在Windows下用MingW 4.5.2编译live555
1.下载live555(http://www.live555.com/liveMedia/public/),解压. 2.进入MingW Shell,输入cd: F:/Qt/live(假定解压到F:/Q ...