CPU模拟,随机化突发、等待和优先级
我在这里编写了一个用于CPU模拟的用户交互代码,在这里用户将输入突发时间的数量、等待时间和优先级。我想删除该功能并将其替换为随机化,它将随机化突发时间、等待时间和优先级的数量。不能有 0(零),也不能有相同的值。我真的需要这个帮助,因为我真的不知道如何随机化。
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.lang.*;
/*
<applet code="finalcase" height=500 width=700>
</applet>
*/
public class finalcase extends Applet{
int apw1[];
int p[];
int i=0,n,n1;
Graphics g= getGraphics();
public void init(){
try{
BufferedReader obj=new BufferedReader(new InputStreamReader (System.in));
System.out.println("ENTER no. of processes:");
n=Integer.parseInt(obj.readLine());
int ch;
int bt[]=new int[n];
do{
System.out.println("MENU FOR CPU SCHEDULING");
System.out.println("1.PRIORITY method (non-preemptive)");
System.out.println("2.PRIORITY method (preemptive)");
System.out.println("3.EXIT");
System.out.println("ENTER YOUR CHOICE");
ch=Integer.parseInt(obj.readLine());
switch(ch){
case 1: //Non-Preemptive Priority
{
for( i=0;i<n;i++){
System.out.println("ENTER burst time for each process: p"+(i+1));
bt[i]=Integer.parseInt(obj.readLine());
}
n1=n;
int pr[]=new int[n];
int pr1[]=new int[n];
float t=0;
int temp;
int turn[]=new int[n];
p=new int[n];
apw1=new int[n+1];
apw1[0]=0;
float tu=0;
for(i=0;i<n;i++){
System.out.println("Enter the priority for p"+(i+1));
pr[i]=Integer.parseInt(obj.readLine());
}
for(i=0;i<n;i++){
pr1[i]=pr[i];
}
for( i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(pr1[i]>pr1[j]){
temp=pr1[i];
pr1[i]=pr1[j];
pr1[j]=temp;
}
for( i=0;i<n;i++)
for(int j=0;j<n;j++)
if(pr1[i]==pr[j])
p[i]=j+1;
for(i=0;i<n;i++){
int k=p[i];
apw1[i+1]=bt[k-1]+apw1[i];
}
for( i=0;i<n;i++){
System.out.println("individual waiting time for process p"+p[i]+"is"+apw1[i]+" ");
}
for( i=0;i<n;i++)
t+=apw1[i];
float avg=t/n;
System.out.println("average waiting time is:"+avg);
for( i=0;i<n;i++){
int k=p[i];
turn[i]=bt[k-1]+apw1[i];
System.out.println("turnaround time for process p"+p[i]+"is"+turn[i]+" ");
}
for( i=0;i<n;i++)
tu+=turn[i];
float avg1=tu/n;
System.out.println("average turn-around time is:"+avg1);
paint(g);
}//end of 1st case
break;
case 2:
{
int total=0,k=0,co=0,small=999,sp=0,sp1=0,x=0,count=0;
int pro[][]=new int[n][4];
int wt[]=new int[n];
int wt1[]=new int[n];
int bt1[]=new int[n];
int tt[]=new int[n];
apw1=new int [20];
p=new int[20];
for(int i=0;i<n;i++){
pro[i][0]=i;
System.out.print("Burst Time for P"+i+" :");
pro[i][3]=Integer.parseInt(obj.readLine());
total+=pro[i][3];
System.out.print("Arrival Time for P"+i+" :");
pro[i][2]=Integer.parseInt(obj.readLine());
System.out.print("Priority for P"+i+" :");
pro[i][1]=Integer.parseInt(obj.readLine());
}
for(int i=0;i<n;i++)
wt1[i]=pro[i][2];
for(int i=0;i<n;i++)
bt1[i]=pro[i][3];
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(pro[i][2]>pro[j][2]) {
int temp[]=pro[i];
pro[i]=pro[j];
pro[j]=temp;
}
for(int i=1;i<=total;i++){
small=999;
for(int j=co;j<n;j++)
if(k>=pro[j][2])
co++;
for(int j=0;j<co;j++){
if(small>pro[j][1]&&pro[j][3]!=0){
small=pro[j][1];
sp=pro[j][0];
sp1=j;
}
}
if(p[x]==sp){
apw1[x+1]++;
}else{
x++;
p[x]=sp;
apw1[x+1]=apw1[x];
apw1[x+1]++;
count++;
}
pro[sp1][3]-=1;
if(pro[sp1][3]==0)
tt[sp1]=i;
for(int j=0;j<n;j++){
if(pro[j][3]!=0&&j!=sp)
wt[j]+=1;
}
k++;
}//end of main for loop
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(pro[i][0]>pro[j][0]){
int temp[]=pro[i];
pro[i]=pro[j];
pro[j]=temp;
int tem=wt[i],tem1=tt[i];
wt[i]=wt[j];
wt[j]=tem;
tt[i]=tt[j];
tt[j]=tem1;
}
System.out.println();
for(int m=0;m<=count;m++)
p[m]+=1;
double awt=0.0,att=0.0;
System.out.println();
for (int i = 0; i<n; i++){
System.out.println("waiting time for process p "+i+" "+(wt[i]-wt1[i]));
awt+=(wt[i]-wt1[i]);
att+=(wt[i]-wt1[i]+bt1[i]);
}
for (int i = 0; i<n; i++){
System.out.println("turnaround time for process p"+i+" "+(wt[i]-wt1[i]+bt1[i]));
}
System.out.println("Average waiting time : "+(awt/n));
System.out.println("Average turnaround time : "+(att/n));
n1=count+1;
paint(g);
}//end of case 2
}//end of switch
}while(ch!=3); //end of do-while loop
}catch (Exception e){
}//end of try-catch
}//end of init method
public void paint(Graphics g){
for(int j=1;j<=n1;j++){
g.drawRect(50,50,(apw1[j]*20),30);
g.drawString("p"+p[j-1],(55+(apw1[j-1]*20)),70);
g.drawString(""+apw1[j-1],50+(apw1[j-1]*20),100);
try {
Thread.sleep(500);
}catch (Exception e) {
;
}//end of try-catch
}//end of for
g.drawString(""+apw1[n1],50+(apw1[n1]*20),100);
}//end of painter
}//end of class
I made a code here which is a user interactive for the CPU simulation, here the user will input the number of burst time, waiting time and priority. I would like to remove the that feature and replace it into randomization, it will randomize the number of burst time, waiting time and priority. There must be no 0 (zeros) and no same values. I really need this help because I really don't know how to randomize.
import java.applet.*;
import java.awt.*;
import java.io.*;
import java.util.*;
import java.lang.*;
/*
<applet code="finalcase" height=500 width=700>
</applet>
*/
public class finalcase extends Applet{
int apw1[];
int p[];
int i=0,n,n1;
Graphics g= getGraphics();
public void init(){
try{
BufferedReader obj=new BufferedReader(new InputStreamReader (System.in));
System.out.println("ENTER no. of processes:");
n=Integer.parseInt(obj.readLine());
int ch;
int bt[]=new int[n];
do{
System.out.println("MENU FOR CPU SCHEDULING");
System.out.println("1.PRIORITY method (non-preemptive)");
System.out.println("2.PRIORITY method (preemptive)");
System.out.println("3.EXIT");
System.out.println("ENTER YOUR CHOICE");
ch=Integer.parseInt(obj.readLine());
switch(ch){
case 1: //Non-Preemptive Priority
{
for( i=0;i<n;i++){
System.out.println("ENTER burst time for each process: p"+(i+1));
bt[i]=Integer.parseInt(obj.readLine());
}
n1=n;
int pr[]=new int[n];
int pr1[]=new int[n];
float t=0;
int temp;
int turn[]=new int[n];
p=new int[n];
apw1=new int[n+1];
apw1[0]=0;
float tu=0;
for(i=0;i<n;i++){
System.out.println("Enter the priority for p"+(i+1));
pr[i]=Integer.parseInt(obj.readLine());
}
for(i=0;i<n;i++){
pr1[i]=pr[i];
}
for( i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(pr1[i]>pr1[j]){
temp=pr1[i];
pr1[i]=pr1[j];
pr1[j]=temp;
}
for( i=0;i<n;i++)
for(int j=0;j<n;j++)
if(pr1[i]==pr[j])
p[i]=j+1;
for(i=0;i<n;i++){
int k=p[i];
apw1[i+1]=bt[k-1]+apw1[i];
}
for( i=0;i<n;i++){
System.out.println("individual waiting time for process p"+p[i]+"is"+apw1[i]+" ");
}
for( i=0;i<n;i++)
t+=apw1[i];
float avg=t/n;
System.out.println("average waiting time is:"+avg);
for( i=0;i<n;i++){
int k=p[i];
turn[i]=bt[k-1]+apw1[i];
System.out.println("turnaround time for process p"+p[i]+"is"+turn[i]+" ");
}
for( i=0;i<n;i++)
tu+=turn[i];
float avg1=tu/n;
System.out.println("average turn-around time is:"+avg1);
paint(g);
}//end of 1st case
break;
case 2:
{
int total=0,k=0,co=0,small=999,sp=0,sp1=0,x=0,count=0;
int pro[][]=new int[n][4];
int wt[]=new int[n];
int wt1[]=new int[n];
int bt1[]=new int[n];
int tt[]=new int[n];
apw1=new int [20];
p=new int[20];
for(int i=0;i<n;i++){
pro[i][0]=i;
System.out.print("Burst Time for P"+i+" :");
pro[i][3]=Integer.parseInt(obj.readLine());
total+=pro[i][3];
System.out.print("Arrival Time for P"+i+" :");
pro[i][2]=Integer.parseInt(obj.readLine());
System.out.print("Priority for P"+i+" :");
pro[i][1]=Integer.parseInt(obj.readLine());
}
for(int i=0;i<n;i++)
wt1[i]=pro[i][2];
for(int i=0;i<n;i++)
bt1[i]=pro[i][3];
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(pro[i][2]>pro[j][2]) {
int temp[]=pro[i];
pro[i]=pro[j];
pro[j]=temp;
}
for(int i=1;i<=total;i++){
small=999;
for(int j=co;j<n;j++)
if(k>=pro[j][2])
co++;
for(int j=0;j<co;j++){
if(small>pro[j][1]&&pro[j][3]!=0){
small=pro[j][1];
sp=pro[j][0];
sp1=j;
}
}
if(p[x]==sp){
apw1[x+1]++;
}else{
x++;
p[x]=sp;
apw1[x+1]=apw1[x];
apw1[x+1]++;
count++;
}
pro[sp1][3]-=1;
if(pro[sp1][3]==0)
tt[sp1]=i;
for(int j=0;j<n;j++){
if(pro[j][3]!=0&&j!=sp)
wt[j]+=1;
}
k++;
}//end of main for loop
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(pro[i][0]>pro[j][0]){
int temp[]=pro[i];
pro[i]=pro[j];
pro[j]=temp;
int tem=wt[i],tem1=tt[i];
wt[i]=wt[j];
wt[j]=tem;
tt[i]=tt[j];
tt[j]=tem1;
}
System.out.println();
for(int m=0;m<=count;m++)
p[m]+=1;
double awt=0.0,att=0.0;
System.out.println();
for (int i = 0; i<n; i++){
System.out.println("waiting time for process p "+i+" "+(wt[i]-wt1[i]));
awt+=(wt[i]-wt1[i]);
att+=(wt[i]-wt1[i]+bt1[i]);
}
for (int i = 0; i<n; i++){
System.out.println("turnaround time for process p"+i+" "+(wt[i]-wt1[i]+bt1[i]));
}
System.out.println("Average waiting time : "+(awt/n));
System.out.println("Average turnaround time : "+(att/n));
n1=count+1;
paint(g);
}//end of case 2
}//end of switch
}while(ch!=3); //end of do-while loop
}catch (Exception e){
}//end of try-catch
}//end of init method
public void paint(Graphics g){
for(int j=1;j<=n1;j++){
g.drawRect(50,50,(apw1[j]*20),30);
g.drawString("p"+p[j-1],(55+(apw1[j-1]*20)),70);
g.drawString(""+apw1[j-1],50+(apw1[j-1]*20),100);
try {
Thread.sleep(500);
}catch (Exception e) {
;
}//end of try-catch
}//end of for
g.drawString(""+apw1[n1],50+(apw1[n1]*20),100);
}//end of painter
}//end of class
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
java.util.Random
类生成随机数。如果生成该数字并获得重复值,请再次生成它。您可以使用任何您喜欢的算法来拒绝数字。
Use the
java.util.Random
class to generate a random number.If you generate the number and get a duplicate, generate it again. You can use whatever algorithm you like to reject numbers.