免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1523 | 回复: 0
打印 上一主题 下一主题

产生字母数字揉合的随机数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-09-21 13:05 |只看该作者 |倒序浏览
项目需要,花了点时间写的工具类,期待您的交流.pninoh@126.com
/**
* 产生随机数工具类
*
* @author huipan
* @date 2007-4-8
* @version v0.1
*/
public class RandomUtil
{
    private static final Logger myLog = Logger.getLogger(RandomUtil.class);
    private static final SecureRandom sr = new SecureRandom();
    /**
     * 产生任意给定长度的随纯机数字字符串
     *
     * @param iLength
     *            产生随机数的位数
     * @return
     */
    public static String getRandomIntNum(int iLength)
    {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i 10) { //TODO }
         */
        if (0 == len)
        {
            return 0;
        }
        StringBuffer sb = new StringBuffer("1");
        for (int i = 0; i  1 && len = limit)
        {
            return maxValue;
        }
        String strTmp = String.valueOf(maxValue);
        int len = strTmp.length();
        int intTmp = getRandomInt(maxValue);
        int[] scope = getScopeOfInt(maxValue);
        if (limit == len)
        {
            return maxValue;/* (intTmp>=scope[0]&&intTmp=scope[0]&&intTmp= min;
    }
    /**
     * 根据已经计算出的字符的下标,得出数字的下标
     *
     * @param totleLimit
     *            生成随机码的总位数
     * @param charIndex
     *            字符下标集合
     * @return
     */
    public static int[] getIndexInsertNum(int totleLimit, int[] charIndex)
    {
        return getPositionNoBody(new int[totleLimit], charIndex);
    }
    /**
     * 得到要插入字符的位置下标
     *
     * @param totleLimit
     *            要产生整个随机码的长度
     * @param charLimit
     *            随即码中包含字符的个数
     * @return
     */
    public static int[] getIndexInsertChar(int totleLimit, int charLimit)
    {
        // 要插入字符的下标集合
        int[] chIndex = new int[charLimit];
        // 赞存处,辅助过滤,存放已经生成的INDEX
        int[] tmp = new int[charLimit];
        // 以会生成的随机数之外的数填充
        for (int ii = 0; ii < tmp.length; ii++)
        {
            tmp[ii] = totleLimit;
        }
        if (totleLimit < charLimit)
        {
            myLog.error("Error::要包含数字位数大于整个随机字符串长度");
            // TODO 是否在外部处理
        }
        int[] scope = getScopeOfInt(totleLimit);
        int comm = totleLimit + 1;
        for (int j = 0, i = 0; i < charLimit;)
        {
            int genNo = getRandomInt(totleLimit);
            if (comm != genNo && isInScope(genNo, scope[0], scope[1]))
            {
                boolean blFlag = false;
                for (int t = 0; t < tmp.length; t++)
                {
                    if (tmp[t] == genNo)
                    {
                        blFlag = true;
                        break;
                    }
                }
                if (blFlag)
                {
                    continue;
                }
                comm = genNo;
                tmp[j] = comm;
                chIndex[j] = comm;
                if (j == charLimit - 1)
                {
                    break;
                }
                i++;
                j++;
            }
        }
        return chIndex;
    }
    /**
     * 需要指定字符大小写的可以订制方法
     * @param limit
     *            总长度限制
     * @param numLimit
     *            含数字长度限制
     * @return 大小写混合字符和数字 串
     */
    public static String genRandomCheckString(int limit, int numLimit)
    {
        /*
         * 生成符合要求的随机数字串
         */
        // String num= getRandomIntNum(numLimit);
        /*
         * 生成符合要求的随机字符串
         */
        // char[] c= getRandomCharArray(limit-numLimit);
        /*
         * 字符插入下标
         */
        int[] indexChar = getIndexInsertChar(limit, limit - numLimit);
        /*
         * 数字插入下标
         */
        // int[] indexInt = getIndexInsertNum(limit, indexChar);
        return new String(componseIntAndCharArray(new char[limit], indexChar,
                getIndexInsertNum(limit, indexChar), getRandomCharArray(limit
                        - numLimit), getRandomIntNum(numLimit).toCharArray()));
    }
    // 得到没有占位的下标
    public static int[] getPositionNoBody(int[] totle, int[] already)
    {
        int lenTotle = totle.length;
        int lenAlready = already.length;
        int[] noBodyPosition = new int[lenTotle - lenAlready];
        for (int index = 0, i = 0; i < lenTotle; i++)
        {
            boolean bFlag = true;
            for (int j = 0; j < lenAlready; j++)
            {
                if (i == already[j])
                {
                    bFlag = false;
                    break;
                }
            }
            if (bFlag && index < noBodyPosition.length)
            {
                noBodyPosition[index++] = i;
            }
        }
        return noBodyPosition;
    }
    /**
     * 合并到最终结果
     *
     * @param destA
     * @param chIndex
     * @param iIndex
     * @param chStore
     * @param iStore
     * @return
     */
    public static char[] componseIntAndCharArray(char[] destA, int[] chIndex,
            int[] iIndex, char[] chStore, char[] iStore)
    {
        int lenDest = destA.length;
        int lenCh = chIndex.length;
        int leniA = iIndex.length;
        // 取小的数
        if (lenDest != (lenCh + leniA) || lenCh != chStore.length
                || leniA != iStore.length)
        {
            // TODO Error
            myLog.error("componse Error .the length is Wrong");
            return null;
        }
        // start to work
        // dest {1,2,3}
        // ch {1} {b}
        // i{2,3} {8,9}
        for (int i = 0; i < lenDest; i++)
        {
            for (int j = 0; j < lenCh; j++)
            {
                if (i == chIndex[j])
                {
                    destA = chStore[j];
                }
            }
            for (int j = 0; j < leniA; j++)
            {
                if (i == iIndex[j])
                {
                    destA = iStore[j];
                }
            }
        }
        return destA;
    }
    public static void main(String[] args)
    {
        System.out.println("\n\ngenCheckString::::"
                + genRandomCheckString(6, 3));
        // System.out.println(getRandomIntNum(10));
        // new NumberUtil().testGetIndexInsertChar();
    }
    private void testComponseIntAndCharArray()
    {
        char[] cA = new char[5];
        int[] iIndex = new int[] { 1, 4 };
        int[] cIndex = new int[] { 0, 2, 3 };
        char[] cStore = "abc".toCharArray();
        char[] iStore = "12".toCharArray();
        System.out.println(new String(componseIntAndCharArray(cA, cIndex,
                iIndex, cStore, iStore)));
    }
    private void test()
    {
        int[] tmp = getPositionNoBody(new int[15], new int[] { 13, 2, 3, 4, 5,
                6 });
        for (int i = 0; i < tmp.length; i++)
        {
            System.out.println(tmp);
        }
    }
    private void testGetIndexInsertChar()
    {
        int[] tmp = getIndexInsertChar(9, 5);
        for (int i = 0; i < tmp.length; i++)
            System.out.print("\nchar::" + tmp);
        int[] tmpOther = getIndexInsertNum(9, tmp);
        for (int i = 0; i < tmpOther.length; i++)
            System.out.print("\nnum::" + tmpOther);
    }
}
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/1073/showart_386927.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP