如何计算得到一个等于用户输入的随机数需要多少次迭代,并用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: %dn", numCalls);
printf("Average number of iterations to find a match: %.2fn", average);
}
return;
}
  1. randoms值没有更新
  2. 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: %dn", numCalls);
average = (float)totalCalls/(float)i;
printf("Average number of iterations to find a match: %.2fn", average);
}
return;
}

这里,您从用户那里获得1个输入,并计算该输入所需的RNG调用数。这个数字除以50不会得到平均值。

您需要输入50次,即scanf语句应该在循环中。如果numCalls[i]是第i个输入所需的随机数生成器调用数,则average是所有50个输入的总和除以50。

除了更新randoms相等性比较的技术故障外,您还必须验证所有用户输入,并且确认所提供的值在您的预期范围内。否则,如果发生匹配输入失败,则有调用未定义行为的风险。

scanf具有返回。您必须使用它来验证格式字符串中包含的每个转换说明符的成功转换(例如"%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: %dn", 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: %dn", 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: %.2fn", avg);
return 0;
}

示例使用/输出

$ ./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

仔细看看,如果你还有问题,请告诉我。

最新更新