C stdlib.h rand()%范围不是从0到范围
我试图从C中的数组中选取随机索引。仅仅从我得到的索引来看,似乎我得到的是1000-1200的数字,而不是期望的范围 下面是调用rand()的代码 我只在主函数中调用srand一次C stdlib.h rand()%范围不是从0到范围,c,random,C,Random,我试图从C中的数组中选取随机索引。仅仅从我得到的索引来看,似乎我得到的是1000-1200的数字,而不是期望的范围 下面是调用rand()的代码 我只在主函数中调用srand一次 int main(int argc, char** argv) { int i; int simulations = 100000; // initialize the process hash table if(argc > 1){ removal_algorit
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);
}
您的代码显然只打印那些索引,对于这些索引,
table[table\u index]
不为空。如果表[0]
为空,它将永远不会打印0
。也就是说,此代码不一定演示rand()
的行为。相反,它的行为可能主要由表的内容决定。这意味着我的表分布不正确。好吧,我很高兴至少现在我发现了这一点。这似乎不是问题所在,但请注意,rand()%根据rand\u MAX
和SOME\u VALUE
的值,某些值可能会有很大的偏差。如果<代码> RANDMAX 不是“代码>某个值< /COD>”的倍数,则如何生成。
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