Math.random();
定义的是一个随机的double型【0,1)的数。
random*(b-a+1)+a;可以生成[a,b+1)之间的数
使用强制转换(int)就可以得到[a,b]之间的整数。
student[] stus=new student[20];
//使用数组来声明变量
for(int i=0;i<stus.length;i++){
stus[i]=new student();
stus[i].number=i+1;
stus[i].state=(int)(Math.random()*(6-1+1)+1);
stus[i].score=(int)(Math.random()*(100+1));
还可用random的nextInt方法来生成整数
Random rand=new Random();
int n1=rand.nextInt(100);//返回值在范围[0,100) 即[0,99]
int n2=rand.nextInt(100)+1;//[1,100]内的随机整数
int n3=rand.nextInt(80)+10;//[10,89]内的随机整数
int n4=rand.nextInt(27)+82;//[82,108]内的随机整数