package fourtenn;
import java.util.Random;
/**
* @author Administrator
*这个类是总结几种基本数据类型随机数的生成
*1、生成正数
*/
public class RandomTest {
public static void main(String[] args) {
//生成100个int型数据,有正数有负数
int [] array = new int[100];
Random a = new Random();
for(int x = 0;x<array.length;x++){
array[x] = a.nextInt();
}
for(int x:array){
sop(x);
}
//生成100个int型数据,要求在100之内,包括0,不包括100;
int [] array1 = new int[100];
Random a1 = new Random();
for(int x = 0;x<array.length;x++){
array1[x] = a.nextInt(10);
}
for(int x:array1){
sop(x);
}
//生成100个int型数据,要求在100之内,包括0,包括100;
int [] array2 = new int[100];
Random a2 = new Random();
for(int x = 0;x<array2.length;x++){
array2[x] = a2.nextInt(10)+1;
}
for(int x:array2){
sop(x);
}
//生成100个double型数据,0-1之间;
double [] array3 = new double[100];
Random a3 = new Random();
for(int x = 0;x<array3.length;x++){
//array3[x] = a3.nextDouble();
array3[x] = Math.random();
}
for(double x:array3){
sop(x);
}
//生成100个double型数据,500000之间;
double [] array4 = new double[100];
Random a4 = new Random();
for(int x = 0;x<array4.length;x++){
//array4[x] = a3.nextDouble();
array4[x] =500000* Math.random();
}
for(double x:array4){
sop(x);
}
//生成100个char型数据,大写;
char [] array5 = new char[100];
for(int x = 0;x<array5.length;x++){
int random=(int) Math.round(Math.random()*25+65);
char temp=(char) random;
System.out.print(temp+" ");}
sop(null);
//生成100个char型数据,小写;
char [] array6 = new char[100];
for(int x = 0;x<array6.length;x++){
int random=(int) Math.round(Math.random()*25+97);
char temp=(char) random;
System.out.print(temp+" ");}
}
public static void sop(Object obj){
System.out.println(obj+" ");
}
}
白菜之几种基本数据类型随机数生成的小结!
最新推荐文章于 2021-12-26 16:37:27 发布