java随机数的生成
package randomexample;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CreateRandomNo3 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
List orgList =new ArrayList();
for(int i=1;i<=100;i++){
orgList.add(i);
}
Collections.shuffle(orgList);
for(int i=0;i<10;i++){
System.out.println(orgList.get(i));
}
}
}
package randomexample;
import java.util.Random;
public class CreateRandomNo2 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int [] orgArray=new int[100];
int temp=0;
int tempRandom=0;
for(int i=1;i<orgArray.length;i++){
orgArray[i-1]=i;
}
//打乱数组
Random rand=new Random();
for(int j=1;j<=3;j++){
for(int i=1;i<=orgArray.length;i++){
temp=orgArray[i-1];
tempRandom=rand.nextInt(100);
orgArray[i-1]=orgArray[tempRandom];
orgArray[tempRandom]=temp;
}
}
for (int i=0;i<10;i++){
System.out.println(orgArray[i]);
}
}
}
package randomexample;
import java.util.Random;
public class CreateRandomNo {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Random rand=new Random();
for(int i=0;i<10;i++){
System.out.println(rand.nextInt(20));
}
}
}