如何计算获得等于用户输入的随机数并计算 C 中的平均值所需的迭代次数?
how to count how many iterations it takes to get a random number equal to user input and calculate that average in C?
使用嵌套循环,我们必须创建一个程序,从用户那里获取 0-99 范围内的数字,并使用种子随机数生成器来尝试猜测数字。在生成用户号码之前,我们必须跟踪 RNG 生成了多少号码。我们还必须执行此过程 50 次,然后计算 RNG 猜测我们的数字所需的平均尝试次数。
这是我到目前为止所拥有的一切,我很难过:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand(time(NULL));
int userInput;
int randoms = rand() % 100;
int numCalls;
int i;
float average = 0.0;
printf("Please enter a number in between 0 and 99: ");
scanf(" %d", &userInput);
for( i = 0; i < 50; i++)
{
while (userInput != randoms);
{
numCalls = numCalls + 1;
if (userInput = randoms)
{
float average = numCalls/50.0;
}
}
printf("Number of iterations it took for a matching number: %d\n", numCalls);
printf("Average number of iterations to find a match: %.2f\n", average);
}
return;
}
- 没有
randoms
值的更新
if (userInput=randoms)
不是布尔函数,应该是if (userInput==randoms)
而不是
这是我的建议:
int main()
{
srand(time(NULL));
int userInput;
int randoms = rand() % 100;
int numCalls;
int i;
float average = 0.0;
int totalCalls=0;
printf("Please enter a number in between 0 and 99: ");
scanf(" %d", &userInput);
for( i = 0; i < 50; i++)
{
numCalls = 0; //reset iteration number
do{
randoms = rand()%100;
numCalls++
} while (randoms!=userInput);
totalCalls += numCalls;
printf("Number of iterations it took for a matching number: %d\n", numCalls);
average = (float)totalCalls/(float)i;
printf("Average number of iterations to find a match: %.2f\n", average);
}
return;
}
在这里,您从用户那里获取 1 个输入,并且只计算该输入所需的 RNG 调用次数。将此数字除以 50 不会得到平均值。
您想输入 50 次,即 scanf 语句应该在循环内。
如果 numCalls[i] 是第 i 个输入所需的随机数生成器调用次数,则平均值是所有 50 个输入的总和除以 50。
除了更新randoms
和相等比较的技术故障外,您必须验证所有用户输入,并且 validate 提供的值在您的预期范围内。否则,如果发生 matching 或 input 失败,您可能会调用 Undefined Behavior。
scanf
有一个 return。您必须使用它来验证 格式字符串 中包含的每个 转换说明符 的成功转换(例如 "%d"
是 1
转换)。如果转换成功,则必须验证该值是否介于 0 - 99
之间。您可以通过以下方式完成:
#define MAXN 99
...
if (scanf ("%d", &num) != 1) { /* validate ALL user input */
fprintf (stderr, "error: invalid input.\n");
return 1;
}
if (num < 0 || num > MAXN) { /* validate num is within range */
fprintf (stderr, "error: value out of range: %d\n", num);
return 1;
}
考虑需要变量的范围。是的,您可以将它们全部声明在顶部,但如果它们仅在循环等中使用,那么如果您在需要的范围内声明值,则可以减少名称冲突或稍后无意中修改值的可能性。 (对于 C99 或更高版本,您可以在循环声明中声明 for
循环变量(例如 for (int i = 0; ...)
)
将这些部分放在一起,您可以通过验证输入来防止 UB,如上所述,并使用类似以下内容整理变量声明的范围:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NTIMES 50 /* if you need a constant, declare one (or two) */
#define MAXN 99
int main (void) {
int i, num;
double avg = 0.0;
srand (time (NULL));
printf ("enter a number (0-%d): ", MAXN);
if (scanf ("%d", &num) != 1) { /* validate ALL user input */
fprintf (stderr, "error: invalid input.\n");
return 1;
}
if (num < 0 || num > MAXN) { /* validate num is within range */
fprintf (stderr, "error: value out of range: %d\n", num);
return 1;
}
for (i = 0; i < NTIMES; i++) { /* loop 50 time for average */
int nrands = 0,
randnum = rand() % (MAXN + 1); /* get 1st random */
/* loop until equal updating counter nrands */
for (; randnum != num; randnum = rand() % (MAXN + 1))
nrands += 1;
printf ("test[%2d] : %4d iterations to match.\n", i+1, nrands);
avg += nrands; /* keep sum in avg */
}
avg /= (double)NTIMES; /* divide for final average */
printf ("\nAverage number of iterations to find match: %.2f\n", avg);
return 0;
}
例子Use/Output
$ ./bin/iters2match
enter a number (0-99): 31
test[ 1] : 19 iterations to match.
test[ 2] : 30 iterations to match.
test[ 3] : 239 iterations to match.
test[ 4] : 116 iterations to match.
test[ 5] : 220 iterations to match.
test[ 6] : 198 iterations to match.
test[ 7] : 64 iterations to match.
test[ 8] : 77 iterations to match.
test[ 9] : 106 iterations to match.
test[10] : 94 iterations to match.
test[11] : 123 iterations to match.
test[12] : 77 iterations to match.
test[13] : 167 iterations to match.
test[14] : 23 iterations to match.
test[15] : 3 iterations to match.
test[16] : 226 iterations to match.
test[17] : 58 iterations to match.
test[18] : 190 iterations to match.
test[19] : 44 iterations to match.
test[20] : 204 iterations to match.
test[21] : 134 iterations to match.
test[22] : 30 iterations to match.
test[23] : 40 iterations to match.
test[24] : 59 iterations to match.
test[25] : 21 iterations to match.
test[26] : 218 iterations to match.
test[27] : 8 iterations to match.
test[28] : 21 iterations to match.
test[29] : 259 iterations to match.
test[30] : 227 iterations to match.
test[31] : 11 iterations to match.
test[32] : 22 iterations to match.
test[33] : 187 iterations to match.
test[34] : 90 iterations to match.
test[35] : 5 iterations to match.
test[36] : 43 iterations to match.
test[37] : 114 iterations to match.
test[38] : 38 iterations to match.
test[39] : 24 iterations to match.
test[40] : 53 iterations to match.
test[41] : 71 iterations to match.
test[42] : 148 iterations to match.
test[43] : 61 iterations to match.
test[44] : 78 iterations to match.
test[45] : 5 iterations to match.
test[46] : 30 iterations to match.
test[47] : 281 iterations to match.
test[48] : 18 iterations to match.
test[49] : 109 iterations to match.
test[50] : 31 iterations to match.
Average number of iterations to find match: 94.28
检查一下,如果您还有其他问题,请告诉我。
使用嵌套循环,我们必须创建一个程序,从用户那里获取 0-99 范围内的数字,并使用种子随机数生成器来尝试猜测数字。在生成用户号码之前,我们必须跟踪 RNG 生成了多少号码。我们还必须执行此过程 50 次,然后计算 RNG 猜测我们的数字所需的平均尝试次数。 这是我到目前为止所拥有的一切,我很难过:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
srand(time(NULL));
int userInput;
int randoms = rand() % 100;
int numCalls;
int i;
float average = 0.0;
printf("Please enter a number in between 0 and 99: ");
scanf(" %d", &userInput);
for( i = 0; i < 50; i++)
{
while (userInput != randoms);
{
numCalls = numCalls + 1;
if (userInput = randoms)
{
float average = numCalls/50.0;
}
}
printf("Number of iterations it took for a matching number: %d\n", numCalls);
printf("Average number of iterations to find a match: %.2f\n", average);
}
return;
}
- 没有
randoms
值的更新 if (userInput=randoms)
不是布尔函数,应该是if (userInput==randoms)
而不是
这是我的建议:
int main()
{
srand(time(NULL));
int userInput;
int randoms = rand() % 100;
int numCalls;
int i;
float average = 0.0;
int totalCalls=0;
printf("Please enter a number in between 0 and 99: ");
scanf(" %d", &userInput);
for( i = 0; i < 50; i++)
{
numCalls = 0; //reset iteration number
do{
randoms = rand()%100;
numCalls++
} while (randoms!=userInput);
totalCalls += numCalls;
printf("Number of iterations it took for a matching number: %d\n", numCalls);
average = (float)totalCalls/(float)i;
printf("Average number of iterations to find a match: %.2f\n", average);
}
return;
}
在这里,您从用户那里获取 1 个输入,并且只计算该输入所需的 RNG 调用次数。将此数字除以 50 不会得到平均值。
您想输入 50 次,即 scanf 语句应该在循环内。 如果 numCalls[i] 是第 i 个输入所需的随机数生成器调用次数,则平均值是所有 50 个输入的总和除以 50。
除了更新randoms
和相等比较的技术故障外,您必须验证所有用户输入,并且 validate 提供的值在您的预期范围内。否则,如果发生 matching 或 input 失败,您可能会调用 Undefined Behavior。
scanf
有一个 return。您必须使用它来验证 格式字符串 中包含的每个 转换说明符 的成功转换(例如 "%d"
是 1
转换)。如果转换成功,则必须验证该值是否介于 0 - 99
之间。您可以通过以下方式完成:
#define MAXN 99
...
if (scanf ("%d", &num) != 1) { /* validate ALL user input */
fprintf (stderr, "error: invalid input.\n");
return 1;
}
if (num < 0 || num > MAXN) { /* validate num is within range */
fprintf (stderr, "error: value out of range: %d\n", num);
return 1;
}
考虑需要变量的范围。是的,您可以将它们全部声明在顶部,但如果它们仅在循环等中使用,那么如果您在需要的范围内声明值,则可以减少名称冲突或稍后无意中修改值的可能性。 (对于 C99 或更高版本,您可以在循环声明中声明 for
循环变量(例如 for (int i = 0; ...)
)
将这些部分放在一起,您可以通过验证输入来防止 UB,如上所述,并使用类似以下内容整理变量声明的范围:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NTIMES 50 /* if you need a constant, declare one (or two) */
#define MAXN 99
int main (void) {
int i, num;
double avg = 0.0;
srand (time (NULL));
printf ("enter a number (0-%d): ", MAXN);
if (scanf ("%d", &num) != 1) { /* validate ALL user input */
fprintf (stderr, "error: invalid input.\n");
return 1;
}
if (num < 0 || num > MAXN) { /* validate num is within range */
fprintf (stderr, "error: value out of range: %d\n", num);
return 1;
}
for (i = 0; i < NTIMES; i++) { /* loop 50 time for average */
int nrands = 0,
randnum = rand() % (MAXN + 1); /* get 1st random */
/* loop until equal updating counter nrands */
for (; randnum != num; randnum = rand() % (MAXN + 1))
nrands += 1;
printf ("test[%2d] : %4d iterations to match.\n", i+1, nrands);
avg += nrands; /* keep sum in avg */
}
avg /= (double)NTIMES; /* divide for final average */
printf ("\nAverage number of iterations to find match: %.2f\n", avg);
return 0;
}
例子Use/Output
$ ./bin/iters2match
enter a number (0-99): 31
test[ 1] : 19 iterations to match.
test[ 2] : 30 iterations to match.
test[ 3] : 239 iterations to match.
test[ 4] : 116 iterations to match.
test[ 5] : 220 iterations to match.
test[ 6] : 198 iterations to match.
test[ 7] : 64 iterations to match.
test[ 8] : 77 iterations to match.
test[ 9] : 106 iterations to match.
test[10] : 94 iterations to match.
test[11] : 123 iterations to match.
test[12] : 77 iterations to match.
test[13] : 167 iterations to match.
test[14] : 23 iterations to match.
test[15] : 3 iterations to match.
test[16] : 226 iterations to match.
test[17] : 58 iterations to match.
test[18] : 190 iterations to match.
test[19] : 44 iterations to match.
test[20] : 204 iterations to match.
test[21] : 134 iterations to match.
test[22] : 30 iterations to match.
test[23] : 40 iterations to match.
test[24] : 59 iterations to match.
test[25] : 21 iterations to match.
test[26] : 218 iterations to match.
test[27] : 8 iterations to match.
test[28] : 21 iterations to match.
test[29] : 259 iterations to match.
test[30] : 227 iterations to match.
test[31] : 11 iterations to match.
test[32] : 22 iterations to match.
test[33] : 187 iterations to match.
test[34] : 90 iterations to match.
test[35] : 5 iterations to match.
test[36] : 43 iterations to match.
test[37] : 114 iterations to match.
test[38] : 38 iterations to match.
test[39] : 24 iterations to match.
test[40] : 53 iterations to match.
test[41] : 71 iterations to match.
test[42] : 148 iterations to match.
test[43] : 61 iterations to match.
test[44] : 78 iterations to match.
test[45] : 5 iterations to match.
test[46] : 30 iterations to match.
test[47] : 281 iterations to match.
test[48] : 18 iterations to match.
test[49] : 109 iterations to match.
test[50] : 31 iterations to match.
Average number of iterations to find match: 94.28
检查一下,如果您还有其他问题,请告诉我。