1

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
Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
pocari
  • 11
  • 1
  • 2

1 Answers1

1

Use the java.util.Random class to generate a random number.

Random r = new Random();
int randomInt = r.nextInt(5) + 1; // int between 1-5
double randomDouble = r.nextDouble(); // between 0.0 and 0.9999999...

If you generate the number and get a duplicate, generate it again. You can use whatever algorithm you like to reject numbers.

Erick Robertson
  • 32,125
  • 13
  • 69
  • 98
  • If, your code is not multi threaded, then use java.util.Random. You can user System.currentTimeMillis() long value. Take last two digit, last four digit etc as your random number. Try further multiplication, devision by some value on those number. You will get well random numbers. – Nikunj Mar 16 '11 at 12:12
  • @nlKUNJ: why would I want to do that? Those are **very** bad random numbers. Try running some simulation 3 times without any interruption and your `currentTimeMillis()` values can very well be the same for all 3 runs! There's no drawback to using `Random` and it does exactly what is expected here. – Joachim Sauer Mar 16 '11 at 12:52
  • thanks, uhm do you know any algorithm to remove duplicates? im using tha java.util.Random – pocari Mar 17 '11 at 00:47
  • Everytime you generate one, just loop through the list you've already generated and see if it's a duplicate. If it is, generate it again. It's not optimal for speed, but it will work. If you're generating so many that you're running out of values, then you should generate all the values in an array and randomize it. – Erick Robertson Mar 17 '11 at 10:41