我正在尝试从C. The problem I'm having is that my random numbers aren't going from the range I expect (from 0 to 2047) as this example says that it will中的数组中选择随机索引。只是从查看索引我得到它似乎我得到的数字从1000-1200而不是所需的范围。
这是调用rand()的代码。
#define MAXPAGES 2048 // maximum number of processes in the system
//until we have a node, pick random indexes.
while(node == NULL){
table_index = rand() % MAXPAGES;
node = table[table_index];
if(node){
fprintf(stderr, "%d\n", table_index);
if(node->current_pages > 0) break;
else node = NULL;
}
}
我在主要功能中只调用一次srand。
int main(int argc, char** argv) {
int i;
int simulations = 100000;
// initialize the process hash table
if(argc > 1){
removal_algorithm = atoi(argv[1]);
if(argc == 3) simulations = atoi(argv[2]);
}
srand(time(NULL));
for (i = 0; i < MAXPAGES; i++) table[i] = NULL;
printf("Random replacement MMU simulation on %d simulations started\n", simulations);
Simulate(simulations);
printf("Random MMU simulation completed. only writing page faults:,");
printf("%d, read / write page faults: %d, total page faults: %d\n", read_miss, write_miss, read_miss + write_miss);
}
示例序列。
1117
1069
1103
1118
1071
1117
1120
1092
1099
1116
1121
1122
1110
1116
1069
1113
1120
1117
1116
1071
1121
1094
1110
1119
1102
1117
1071
1108
1102
1099
1109
1109
1092
1109
1120
1108
1094
1101
1122
1086
1110
1108
1119
1120
1092
1113
1117
1121
答案 0 :(得分:1)
您的代码显然只打印那些table[table_index]
不为空的索引。如果table[0]
为空,则永远不会打印0
。即此代码不一定证明rand()
的行为。相反,它的行为可能主要取决于table
的内容。