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 的任意组合的更多相关文章

  1. 用js正则判断输入的两位字符,第一位是数字16进制的,第二位是I、O、Q、U除外的字母(A到Z)

    项目中遇到客户的需求变化,要验证某个数据的正确性,判断输入的两位字符,第一位是数字16进制的,第二位是I.O.Q.U除外的字母(A到Z). 本来对js不熟练,网上参考了一下js正则验证的规则,使用正则 ...

  2. ruby中的\z与\Z区别

    s = "this is\nthe name\n" puts "--------------" puts s.match(/name\Z/) puts s.ma ...

  3. 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.注意一点最好用 ...

  4. js随机生成字母数字组合的字符串 随机动画数字

    效果描述: 附件中只有一个index.html文件有效 其中包含css以及html两部分内容 纯js生成的几个随机数字 每次都不重复,点击按钮后再次切换 使用方法: 1.将css样式引入到你的网页中 ...

  5. 【BZOJ】2038: [2009国家集训队]小Z的袜子(hose)(组合计数+概率+莫队算法+分块)

    http://www.lydsy.com/JudgeOnline/problem.php?id=2038 学了下莫队,挺神的orzzzz 首先推公式的话很简单吧... 看的题解是从http://for ...

  6. QTP生成随机数字+字母

    以下函数实现随机生成17位数(包括字母和数字),仍有改进的空间,可根据具体要求适当修改 Dim targetstring '调用返回函数给变量.Function过程通过函数名返回一个值 targets ...

  7. [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+ ...

  8. 教你一招:EXCEL单元格随机生成字母

    =CHAR(RANDBETWEEN(1,4)+65) 65代表大写字母A,依次类推 1代表从A开始 4代表到D结束

  9. php 随机显示据今天30天内的任意一天

    function randomDate() { //echo date( "Y-m-d H:m:s", $newtime); //echo date("Y-m-d H:m ...

随机推荐

  1. JSON数据理解

    话说JSON数据平常用的确实挺多的,但是基本上只知道怎么用,对其一些细节并没有整理过,今儿趁着下午有点空,坐下来,学习整理下,并分享出来. 对于JSON,首先它只是一种数据格式,并非一种语言,虽然和j ...

  2. java.net.BindException: 权限不够

    在Linux 下 ,今天写了个socket小程序,绑定80端口,发现抛异常 原因其实是因为在linux下,如果使用1024以下的端口则需要root权限, 所以因为我当前使用的不是root权限,所以权限 ...

  3. 20150503-struts2入门-标签

    一.几个标签介绍 1.property标签 property标签用于输出指定值: <s:set name="name" value="'kk'" /> ...

  4. hdoj1584 蜘蛛牌 (区间型动态规划)

    hdoj1584 分析: f[i][j] 表示 把一串牌 牌 i 到 j 摞为一摞时 所花费最少的步数. d[i][j] 表示把牌 i 挪到牌 j 上时需要走的步数(最初给的状态). 以一串牌 3~8 ...

  5. tomcat 6 不支持jsf2.2,仅支持jsf2.0及以下版本

    tomcat 6 不支持jsf2.2,仅支持jsf2.0及以下版本 安装tomcat8即可.

  6. ubuntu中安装monodevelop

    sudo apt-get install monoDevelop sudo apt-get install build-essentialsudo apt-get install mono-devel ...

  7. iOS block的使用

    明明知道block是一个很重要的知识点,很久不用就又忘了,这是在网上看到的一个例子.(晚上回去整理另外的一个) 在视图A上有一个按钮(用来在点击的时候推出视图b)和一个label(用来显示从b传回来的 ...

  8. (已实现)相似度到大数据查找之Mysql 文章匹配的一些思路与提高查询速度

    需求,最近实现了文章的原创度检测功能,处理思路一是分词之后做搜索引擎匹配飘红,另一方面是量化词组,按文章.段落.句子做数据库查询,功能基本满足实际需求. 接下来,还需要在海量大数据中快速的查找到与一句 ...

  9. mybatis关联查询

    场景:一个人有多个手机号,一个手机号对应一个人 CREATE TABLE test.mobile ( mid INT NOT NULL auto_increment, tel ), pid INT, ...

  10. 高能物理/HyperPhysics的网站/Website

    参考: 基础物理-高能物理[Hyperphysics]