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

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

 

你可能感兴趣的:(随机数)