首页

关于if-util-3.2.8源码包RandomUtil随机数工具类实现常用默认一千万、指定范围或长度及字符的随机生成等

标签:RandomUtil,随机数工具类,bill99,util,if-util,字符串随机     发布时间:2018-01-28   

一、前言

基于if-util-3.2.8.jar开源包中的com.bill99.seashell.common.util.RandomUtil随机数工具类对指定访问1000万随机生成、指定访问lower~upper随机生成、设定长度len生成及字符随机生成randomChar等常用方式。

二、源码说明

package com.bill99.seashell.common.util;@b@@b@import java.util.Random;@b@@b@public final class RandomUtil@b@{@b@  private static Random random = new Random();@b@@b@  public static int random()@b@  {@b@    return (int)(Math.random() * 10000000.0D);@b@  }@b@@b@  public static int random(int lower, int upper)@b@  {@b@    int num;@b@    if ((lower < 0) || (upper < 0)) {@b@      throw new IllegalArgumentException();@b@    }@b@@b@    int len = String.valueOf(upper).length();@b@    int base = 1;@b@    for (int i = 0; i < len; ++i) {@b@      base *= 10;@b@    }@b@@b@    do@b@      num = (int)(Math.random() * base);@b@    while ((num < lower) || (num > upper));@b@    return num;@b@  }@b@@b@  public static String random(int len)@b@  {@b@    String sys = "23456789ABCDEFGHIJKLMNQRSTUWXYZ";@b@@b@    String re = "";@b@    for (int i = 0; i < len; ++i) {@b@      int n = random.nextInt(31);@b@      re = re + sys.substring(n, n + 1);@b@    }@b@    return re; }@b@@b@  public static String randomDegital(int len) {@b@    StringBuffer result = new StringBuffer();@b@    for (int i = 0; i < len; ++i)@b@      result.append(random.nextInt(10));@b@@b@    return result.toString();@b@  }@b@@b@  public static String randomString(int len)@b@  {@b@    String result = "";@b@@b@    for (int i = 0; i < len; ++i)@b@      result = result + randomChar();@b@@b@    return result;@b@  }@b@@b@  public static char randomChar()@b@  {@b@    int val = random.nextInt(95) + 32;@b@@b@    if ((val == 48) || (val == 79) || (val == 105) || (val == 111))@b@      ++val;@b@    return (char)val;@b@  }@b@}