-
Notifications
You must be signed in to change notification settings - Fork 1
/
RandomAlgorithm.java
106 lines (98 loc) · 2.18 KB
/
RandomAlgorithm.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/**
* This is an implementation of one of the paging algorithms
* TODO: Unlike the other two classes that implement an algorithm, this class has a constructor. While this may be the best way to implement the algorithm, its differences are such that I must wonder if they are to great to be rectified without some noteworthy edits. An MMSimAlgorithm abstract superclass that each algorithm must be a subtype of may make things more uniform, as well as make this code easier to improve in the future.
*
*/
public class RandomAlgorithm
{
int[] pages;
int[] resSet;
Boolean inResSet;
int numInRes;
int pageFaults;
/**
* This is the constructor for the class.
* @param len
* @param resSize
*/
public RandomAlgorithm(shortProgram len, int resSize)
{
pages=len.getArray();
resSet=new int[resSize];
inResSet=false;
numInRes=0;
genStringRan();
pageFaults=0;
}
/**
* This method generates random numbers for your page array.
*/
private void genStringRan()
{
for(int i=0;i<pages.length; i++)
{
int inty=(int) Math.round(Math.random()*99);
pages[i]= inty;
}
}
/**
* This method simulates paging.
* @return
*/
public int simulateDemandPagingRandomAlgorithm()
{
for(int i=0;i<pages.length;i++)
{
inResSet=false;
checkIfInRes(pages[i]);
if(inResSet==true){continue;}
if(numInRes<resSet.length)
{resSet[numInRes]=pages[i];
numInRes++;
}
else
{
implementRandomAlgorithm(pages[i]);
}
pageFaults++;
}
return pageFaults;
}
/**
* This method checks if a page is in the resSet.
* @param s
*/
private void checkIfInRes(int s)
{
int i;
for(i=0;i<resSet.length; i++)
{
if(resSet[i]==s)
{
inResSet=true;
break;
}
}
}
/**
* Implements the random algorithm.
* @param s
*/
private void implementRandomAlgorithm(int s)
{
int rando= (int)Math.round(Math.random()*resSet.length);
if(rando==resSet.length)
{
rando--;
}
resSet[rando]=s;
}
/**
* This method returns the number of page faults.
* @return
*/
public int getNumPageFaults()
{
return pageFaults;
}
}