博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
随机产生字母a--z, A-Z 的任意组合
阅读量:4684 次
发布时间:2019-06-09

本文共 2694 字,大约阅读时间需要 8 分钟。

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;
    }
}

转载于:https://www.cnblogs.com/dragonflyyi/p/4403579.html

你可能感兴趣的文章
SEO知识图一
查看>>
USACO hamming
查看>>
[开源JVM] yvm - 自制Java虚拟机
查看>>
Open vSwitch安装
查看>>
HashMap、HashTable、LinkedHashMap和TreeMap用法和区别
查看>>
document.domain 跨域问题[转]
查看>>
【Android】 No Activity found to handle Intent.
查看>>
Mysql 模糊匹配(字符串str中是否包含子字符串substr)
查看>>
Struts2 Action名称的搜索顺序
查看>>
C++ sort简单用法
查看>>
Oracle分区索引
查看>>
4.17上午
查看>>
IIS的ISAPI接口简介
查看>>
python:open/文件操作
查看>>
16 乘法口诀输出
查看>>
mac 常用地址
查看>>
鼠标经过切换图片
查看>>
流程控制 Day06
查看>>
Linux下安装Tomcat
查看>>
windows live writer 2012 0x80070643
查看>>