[代码]java实现生成重复或不重复的随机数
<不重复>
List list = new ArrayList();
java.util.Random random = new java.util.Random();
while (list.size() < 100) { //假定产生100个随机数
Integer ii = new Integer(random.nextInt(20)); //随机数都在20以内
if (!list.contains(ii)) {
list.add(ii);
}
System.out.println( "随机数产生结果" + list.toString());
}
<可重复>
1.public class RandomGen {
2.
3. private static Random random = new Random();
4.
5.
6. public static String getRandomNumber(String[] range,int rangeLength,int length){
7. StringBuffer sb = new StringBuffer();
8. for(int i=0;i<length;i++){
9. sb.append(String.valueOf(range[random.nextInt(rangeLength)]));
10. }
11. return sb.toString();
12. }
13. public static void main(String[] args) {
14. String[] range = {"0","1","2","3","4","5","6","7","8","9"};
15. System.out.println(getRandomNumber(range, 10, 8));
16.
17. }
18.}