码迷,mamicode.com
首页 > 其他好文 > 详细

产生随机数和随机字母的工具类

时间:2015-06-01 16:24:40      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

package com.souvc.utils;

public class RandomGenerator {
    public RandomGenerator() {
    }
    public static String getRandomNum(int number){
        String password = "";
        for (int i = 0; i < number; i++) {
            String rand = String.valueOf((int) (Math.random() * 10));
            password += rand;

        }
        return password;

    }
    public static String getRandom8Num() {
        String password = "";
        for (int i = 0; i < 8; i++) {
            String rand = String.valueOf((int) (Math.random() * 10));
            password += rand;

        }
        return password;
    }
    public static String randomPassword() {
        return randomString(6);
    }

    public static String randomString(int length) {
        return randomString(length, true);
    }

    public static String randomString(int length, boolean includeNumbers) {
        StringBuffer b = new StringBuffer(length);
        for (int i = 0; i < length; i++)
            if (includeNumbers)
                b.append(randomCharacter());
            else
                b.append(randomAlpha());
        return b.toString();
    }

    public static char randomCharacter() {
        int i = (int) (Math.random() * 3D);
        if (i < 2)
            return randomAlpha();
        else
            return randomDigit();
    }

    public static char randomAlpha() {
        int i = (int) (Math.random() * 52D);
        if (i > 25)
            return (char) ((97 + i) - 26);
        else
            return (char) (65 + i);
    }

    public static char randomDigit() {
        int i = (int) (Math.random() * 10D);
        return (char) (48 + i);
    }
    public static void main(String[] args) {
        System.out.println(getRandom8Num());
    }
}

 

产生随机数和随机字母的工具类

标签:

原文地址:http://www.cnblogs.com/liuhongfeng/p/4543921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!