实验内容:
① 设计一“真随机数”方法:public static int MyRandom(int n,int m);
功能:n<m,随机产生n~m之间的整数,要求每次调用返回一随机数,均匀分布在n~m之间,注意程序每次运行,其产生的随机数是不同的。
② 主程序中循环调用10000次MyRandom(1,100),求随机数分布情况。
并输出随机数落在1-50区间和51-100区间的次数。
③ 制作一“35选7 ” 福利彩券摇奖器,即:每次运行,随机产生7个1~35的不重复的整数。
④ 制作一“x选y ”通用福利彩券摇奖器,即:每次运行,随机产生y个1~x的不重复的整数。
注:此处请按实验要求改写,语句精炼。说明实验题的内容及基本要求。
实验步骤:(实验设计,关键代码。实验结果需截图)
注:此处请注意写出程序设计思路,各功能模块的实现算法描述,对所附关键代码要注释,写明使用的实验数据及测试结果,并对运行结果给出截图。截图方法:使用Alt+PrtSc键获得窗口的截图,粘贴即可。
import java.util.Scanner;
import java.security.SecureRandom;
public class P1 {
public static void main(String[] args) {
System.out.println(MyRandom(1,5));
cal_random();
sum_random();
lucky_drawer_1_35();
lucky_drawer();
}
//生成n-m的随机数
public static int MyRandom(int n,int m){
//判断输入是否正确
if(n>m){
System.out.println("incorrect input!");
System.exit(0);
}
//使用真随机数函数
SecureRandom random = new SecureRandom();
int number;
int randomInt;
int range = m-n;
randomInt = random.nextInt(range+1);
number = n+randomInt;
return number;
}
/*public static int MyRandom(int n,int m){
int number = (int)(Math.random()*(m-n+1) + n);
return number;
}
*/
//计算10000个随机数数中1——100的出现的次数并打印
public static void cal_random(){
//使用数组计数
int[] arr = new int[100];
for (int i = 1; i < 10001; i++) {
int value = MyRandom(1, 100);
arr[value-1]++;
}
//遍历数组打印次数
for(int i = 0; i < 100 ;i++){
System.out.println((i+1)+":"+arr[i]+"次");
}
}
//计算位于1-50和51-100的数的个数并打印
public static void sum_random() {
int sum1 = 0;
int sum2 = 0;
for (int i = 1; i < 10001; i++) {
int value = MyRandom(1, 100);
if (value >= 1 && value <= 50) {
//1-50之间的数计数
sum1++;
} else if(value >= 51 && value <= 100) {
//51-100之间的数计数
sum2++;
}
}
System.out.println("1-50:" + sum1 + "次");
System.out.println("50-100:" + sum2 + "次");
}
//1-35中随机生成7个不重复的随机数并打印
public static void lucky_drawer_1_35(){
int a_number;//存放生成的随机数
int b_number;//存放random中取到的数
int random[] = new int[35];
int arr[] = new int[7];
//将1-35存入长度为35的数组中
for(int i = 0;i < 35;i++){
random[i] = i+1;
}
int x = 0;//计数器
while(x != 7) {
a_number = MyRandom(1, 35);
b_number = random[a_number - 1];
if(b_number == 0){
//取到random数组中值为零的跳过循环
continue;
}else{
//将取到的随机数存入arr数组中
arr[x] = a_number;
//将取过的数组的值变为零(做标记以去重复)
random[a_number - 1] = 0;
x++;
}
}
//便利打印arr中7个数的值
System.out.println("");
for(int i=0; i < 7;i++) {
System.out.print(arr[i]);
}
}
//1-x个数中随机生成y个随机数并打印
public static void lucky_drawer() {
Scanner sc = new Scanner(System.in);
System.out.println("please imput the maximum:");
int x = sc.nextInt();
System.out.println("please imput the number of random numbers:");
int y = sc.nextInt();
if(y>x){
System.out.println("Incorrect input,please try again");
lucky_drawer();
}
int a_number;//存放生成的随机数
int b_number;//存放random中取到的数
int random[] = new int[x];
int arr[] = new int[y];
//将1-x存入长度为x的数组中
for(int i = 0;i < x;i++){
random[i] = i+1;
}
int o = 0;//计数器
while(o != y) {
a_number = MyRandom(1,x);
b_number = random[a_number - 1];
if(b_number == 0){
//取到random数组中值为零的跳过循环
continue;
}else{
//将取到的随机数存入arr数组中
arr[o] = a_number;
//将取过的数组的值变为零(做标记以去重复)
random[a_number - 1] = 0;
o++;
}
}
//便利打印arr中y个数的值
for(int i=0; i < y;i++) {
System.out.println(arr[i]);
}
}
}
1:101次
2:80次
3:97次
4:113次
5:76次
6:103次
7:87次
8:102次
9:106次
10:104次
11:112次
12:94次
13:91次
14:103次
15:95次
16:102次
17:102次
18:89次
19:121次
20:104次
21:97次
22:102次
23:98次
24:100次
25:109次
26:94次
27:103次
28:99次
29:102次
30:91次
31:94次
32:107次
33:76次
34:110次
35:114次
36:116次
37:115次
38:114次
39:100次
40:109次
41:90次
42:82次
43:102次
44:106次
45:87次
46:88次
47:95次
48:84次
49:109次
50:82次
51:97次
52:119次
53:100次
54:120次
55:100次
56:108次
57:91次
58:87次
59:82次
60:88次
61:103次
62:95次
63:116次
64:94次
65:110次
66:104次
67:112次
68:116次
69:90次
70:82次
71:101次
72:97次
73:101次
74:110次
75:94次
76:111次
77:98次
78:92次
79:116次
80:101次
81:85次
82:96次
83:101次
84:111次
85:104次
86:109次
87:88次
88:87次
89:79次
90:109次
91:89次
92:109次
93:113次
94:113次
95:93次
96:108次
97:99次
98:110次
99:108次
100:97次
1-50:4982次
50-100:5018次
实验小结:(主要介绍程序的完成情况,重点、难点以及解决方法,有待改进之处,以及有何收获,体会等)
1、
完成情况:已完成
重点:真随机数
难点:对SecuerRandom的熟练使用
解决方法:使用SecuerRandom包实现
体会:四道题中最难的题目
2、
完成情况:已完成
重点:存储每个数出现的次数,并且存储两个范围内数字出现的次数
难点:如何存储100个次数,存储两个范围内数字出现的次数
解决方法:运用数组存储100个数出现的次数,运用if语句分成两部分讨论
体会:学会运用数组存储数据,学会运用if语句分类讨论
3、
完成情况:已完成
重点:去重复
难点:去重复
解决方法:运用两个数组存储,random存储1-35之间的数,arr存放取到的随机数。循环体内,如果取过则将random中的数赋值为0做标记,若取到的数组值为零,则使用continue跳出本次循环,直到赋值过7次为止。
体会:学会运用循环、做标记和if语句去重复的方法
4、
完成情况:已完成
重点:去重复
难点:去重复
解决方法:运用两个数组存储,random存储1-x之间的数,arr存放取到的随机数,循环体内,如果取过则将random中的数赋值为0做标记,若取到的数组值为零,则使用continue跳出本次循环,直到赋值过y次为止。
体会:学会运用循环、做标记和if语句去重复的方法