GetRondom.java
1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.espeed.yxy.tool;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/***
*
* @author xieyong
* 获取随机码
*/
public class GetRondom {
//生成随机码
public static List<String> getRandom(List<String> value){
List<String> newSmtpList=new ArrayList();
int n = value.size();
Random rand = new Random();
boolean[] bool = new boolean[n];
int num =0;
for (int i = 0; i<n; i++){
do{
//如果产生的数相同继续循环
num = rand.nextInt(n);
}while(bool[num]);
bool[num] =true;
newSmtpList.add(value.get(num));
}
return newSmtpList;
}
public static void main(String[] args) {
List<String> oldStr=new ArrayList<String>();
for(int i=0;i<3;i++){
oldStr.add("name"+i);
}
List<String> newStr=getRandom(oldStr);
for(int j=0;j<newStr.size();j++){
System.out.println(newStr.get(j));
}
}
}