Java examples for java.util:Random Int
get Random Number in range
//package com.java2s; import java.util.Random; public class Main { public static Integer getRandomNum(int max, int min) { Random random = new Random(); int result = random.nextInt(max) % (max - min + 1) + min; return result; }/*from ww w . ja v a2 s. co m*/ public static String getRandomNum(String prefix, int length) { StringBuilder builder = new StringBuilder(); builder.append(prefix).append(getRandomNum(length)); return builder.toString(); } public static String getRandomNum(int length) { Random random = new Random(); StringBuffer buffer = new StringBuffer(); String len = prefixZero(1, length); int strLen = len.length(); int num = strLen % 10 == 0 ? strLen / 10 : (strLen / 10) + 1; int mode = strLen % 10; if (length < 10) { int randomNum = random.nextInt(Integer.parseInt(prefixZero( mode, mode))); int baseNum = Integer.parseInt(prefixZero(1, mode)); buffer.append(randomNum + baseNum); } else { for (int i = 1; i <= num; i++) { if (i < num) { buffer.append(random.nextInt(1000000000) + 1000000000); } else if (i == num && mode != 0) { int randomNum = random.nextInt(Integer .parseInt(prefixZero(mode, mode))); int baseNum = Integer.parseInt(prefixZero(1, mode)); buffer.append(randomNum + baseNum); } else if (i == num && mode == 0) { buffer.append(random.nextInt(1000000000) + 1000000000); } } } return buffer.toString(); } public static String prefixZero(int prefix, int length) { return new StringBuilder() .append(prefix) .append(String.format( "%0$0" + (length - new Integer(prefix).toString() .length()) + "d", 0)).toString(); } }