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. aggregation 详解3(bucket aggregation)

    概述 桶分聚合不进行权值的计算,他们对文档根据聚合请求中提供的判断条件(比如:{"from":0,  "to":100})来进行分组(桶分). 桶分聚合还会额外 ...

  2. [转]SQL Server 和Oracle 数据类型对应

    本文转自:http://blog.sina.com.cn/s/blog_681cd80d0100q84t.html SqlServer 2k转换为Oracle 10g 列名 SqlServer数据类型 ...

  3. java中volatile关键字的含义 (转载)

    在java线程并发处理中,有一个关键字volatile的使用目前存在很大的混淆,以为使用这个关键字,在进行多线程并发处理的时候就可以万事大吉. Java语言是支持多线程的,为了解决线程并发的问题,在语 ...

  4. jQuery UI - draggable 中文API

    ·概述 在任何DOM元素启用拖动功能.通过单击鼠标并拖动对象在窗口内的任何地方移动. 官方示例地址:http://jqueryui.com/demos/draggable/ 所有的事件回调函数都有两个 ...

  5. HTTP层 —— 路由

    1.基本路由 最基本的 Laravel 路由只接收一个 URI 和一个闭包,并以此提供一个非常简单且优雅的定义路由方法: Route::get('foo', function () { return ...

  6. Microsoft Visual Studio 2013 Update 1 离线安装程序

    ☆ 微软官网地址:☆http://www.microsoft.com/zh-cn/download/details.aspx?id=41650☆ 离线安装程序 直接下载链接:☆http://downl ...

  7. Java语言编写计算器(简单的计算器)

    Java编写的一个简单计算器,本人还比较菜,只能这样了,有点代码冗余,不能连续计算. import javax.swing.*; import java.awt.*; import java.awt. ...

  8. C# is 强制转换

    在平时开发中,经常遇上强制转换,在这过程中经常遇上null对象转换为值类型,如果不判断的情况下在编译的时候不会出错,但程序一运行就抛出错误.好在C#为我们提供了is ,它判断一个对象如果成立就转换,如 ...

  9. 【转】Error: no `server' JVM at `C:\Program Files\Java\jre6\bin\server\jvm.dll'.解决办法

    出现问题: 用java -jar XXX.jar -server -Xms900m -Xmx900m 或者 java -server -version 提示错误 Error: no `server' ...

  10. android adb:电池与电量

    手机每个硬件的耗电量是不一样的!比如屏幕就是耗电大户!其它一些元件则耗电量非常小! 使用android dumpsys工具可以获取电池以及电量消耗信息! dumpsys工具:battery.batte ...