Here you can find the source of randomCommonStr(int min, int max, int n)
public static String randomCommonStr(int min, int max, int n)
//package com.java2s; public class Main { public static String randomCommonStr(int min, int max, int n) { int[] arr = randomCommon(min, max, n); String tmp = ""; for (int i = 0; i < arr.length; i++) { tmp += arr[i] + ""; }//from w w w.j ava2s . c o m return tmp; } public static int[] randomCommon(int min, int max, int n) { if (max == min + n) { return null; } if (n > (max - min + 1) || max < min) { return null; } int[] result = new int[n]; int count = 0; while (count < n) { int num = (int) (Math.random() * (max - min)) + min; boolean flag = true; for (int j = 0; j < n; j++) { if (num == result[j]) { flag = false; break; } } if (flag) { result[count] = num; count++; } } return result; } }