我是靠谱客的博主 舒服小懒虫,最近开发中收集的这篇文章主要介绍java 产生随机数_java生成随机数,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

packagecom.rd.p2p.common.util;importjava.util.Iterator;importjava.util.LinkedHashSet;importjava.util.Random;importjava.util.Set;importjava.util.UUID;/*** 工具类-随机数*/

public classRandomUtil {/*** 获取从a至z,长度为length随机数

*

*@return

*/

public static String getRandomStr(intlength) {

String base= "abcdefghijklmnopqrstuvwxyz";

Random random= newRandom();

StringBuffer sb= newStringBuffer();for (int i = 0; i < length; i++) {int number =random.nextInt(base.length());

sb.append(base.charAt(number));

}returnsb.toString();

}/*** 获取范围内int值

*

*@param获取范围内int值

*@return

*/

public static int getRandomRange(int max, intmin) {return (int) (Math.random() * (max - min) +min);

}/*** 获取随机长度随机字符

*

*@paramlength base

*@return

*/

public static String getRandomString(int length, String base) { //length表示生成字符串的长度

Random random = newRandom();

StringBuffer sb= newStringBuffer();for (int i = 0; i < length; i++) {int number =random.nextInt(base.length());

sb.append(base.charAt(number));

}returnsb.toString();

}/*** 获取随机长度随机字符

*

*@paramlength

*@return

*/

public static String getRandomString(int length) { //length表示生成字符串的长度

String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

Random random= newRandom();

StringBuffer sb= newStringBuffer();for (int i = 0; i < length; i++) {int number =random.nextInt(base.length());

sb.append(base.charAt(number));

}returnsb.toString();

}/*** 获取随机长度随机数字

*

*@paramlength

*@return

*/

public static String getRandomNumString(int length) { //length表示生成字符串的长度

String base = "0123456789";

Random random= newRandom();

StringBuffer sb= newStringBuffer();for (int i = 0; i < length; i++) {int number =random.nextInt(base.length());

sb.append(base.charAt(number));

}returnsb.toString();

}/*** 返回随机数组

*

*@paramstart 开始值

*@paramend 结束值

*@return

*/

public static int[] getRangRandom(int start, intend) {return getRangRandom(start, end, end - start + 1);

}/*** 返回指定范围指定个数的不重复随机数。

*

*@paramstart

*@paramend

*@paramnum

*@return

*/

public static int[] getRangRandom(int start, int end, intnum) {int length = end - start + 1;//参数不合法

if (length < 1 || num >length) {return null;

}else{int[] numbers = new int[length];int[] result = new int[num];//循环赋初始值

for (int i = 0; i < length; i++) {

numbers[i]= i +start;

}

Random random= newRandom();//取randomMax次数

for (int i = 0; i < num; i++) {//随机获取取数的位置

int m = random.nextInt(length - i) +i;

result[i]=numbers[m];//交换位置

int temp =numbers[m];

numbers[m]=numbers[i];

numbers[i]=temp;

}returnresult;

}

}/** 生成6位随机数验证码*/

public staticString code() {

Set set =getRandomNumber();

Iterator iterator =set.iterator();

String temp= "";while(iterator.hasNext()) {

temp+=iterator.next();

}returntemp;

}public static SetgetRandomNumber() {

Set set = new LinkedHashSet();

Random random= newRandom();while (set.size() < 6) {

set.add(random.nextInt(10));

}returnset;

}public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f","g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s","t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5","6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I","J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V","W", "X", "Y", "Z"};public static String getShortUUID(intnum) {

StringBuffer shortBuffer= newStringBuffer();

String uuid= UUID.randomUUID().toString().replace("-", "");for (int i = 0; i < num; i++) {

String str= uuid.substring(i * 4, i * 4 + 4);int x = Integer.parseInt(str, 16);

shortBuffer.append(chars[x% 0x3E]);

}returnshortBuffer.toString();

}

}

最后

以上就是舒服小懒虫为你收集整理的java 产生随机数_java生成随机数的全部内容,希望文章能够帮你解决java 产生随机数_java生成随机数所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部