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. [转]Unable to build: the file dx.jar was not loaded from the SDK folder!

    本文转自:http://www.developerbits.com/tag/unable-to-build-the-file-dx-jar-was-not-loaded-from-the-sdk-fo ...

  2. [转]JSON序列化与反序列化

    本文转自:http://www.cnblogs.com/ejiyuan/archive/2010/04/09/1708084.html 方法一:引入System.Web.Script.Serializ ...

  3. MSP430常见问题之工作模式和功耗类

    Q1:1,MSP430进入LP 模式后,CPU 停止运行,那么,进入中断执行退出后,由于SR的恢复,导致还处于LP 模式,是否意味着,CPU 在退出中断后立即停止了呢?2,也就是说,进入LP 模式后, ...

  4. 【linux操作命令】crontab

    带续写... 版权声明:本文为博主原创文章,未经博主允许不得转载.

  5. MySQL高可用解决方案(MySQL HA Solution)

    http://blog.sina.com.cn/s/blog_7e89c3f501012vtr.html 什么是高可用性?很多公司的服务都是24小时*365天不间断的.比如Call Center.这就 ...

  6. HDFS之SequenceFile和MapFile

    http://blog.csdn.net/javaman_chen/article/details/7241087 Hadoop的HDFS和MapReduce子框架主要是针对大数据文件来设计的,在小文 ...

  7. 过滤网页中HTML代码的ASP函数

    Function LoseHtml(ContentStr) Dim ClsTempLoseStr,regEx ClsTempLoseStr = Cstr(ContentStr) Set regEx = ...

  8. asp快速开发方法之分页函数

    log_Content "自己常用的ASP分页代码,将以下代码放入你的函数文件内,在使用的文件内写上<!--#include file="调用文件.asp" /&g ...

  9. asp图片化电话号码,避免蜘蛛之类爬走用户隐私

    作用:将页面中的电话号码生成图片格式.挺多的分类信息类网站使用这个功能.不用真正的生成图片.原理类似验证码,挺不错的. <% Call Com_CreatValidCode(Request.Qu ...

  10. HW--漂亮度2(测试通过)

    总结:几个函数的使用 (1)  int num=Integer.parseInt(str[0]); //将第一个字符串转成整形数,表示名字个数 (2) String string1=str[i].to ...