0

i have to generate random number of antigen tests, then for each test i have to generate a number again, either 1 or 0. In this code, it generates a random value 0 or 1, but applies the value to every test. How do i make it different for each test? (we have to use functions)

#include<stdio.h>
#include<time.h>
#include<stdlib.h>

int main()
{
    srand(time(NULL));
    int testResult();
    float positivePercentage(int, int);
    void displayResults(int,int,float);
    int result;
    int i;
    int negativeResult = 0;
    int positiveResult = 0;
    int numofTests;
    int c=0;

    srand(time(NULL));
    numofTests = 1+(int) rand()%100;
    printf("You have completed %d test today, and the results are as follows:\n",numofTests);

    for (i=1; i<=numofTests; i++)
    {

        //testResult(&result);
        printf("test %d: %d\n", i, testResult());

    }
return 0;
}
    int testResult()
{

    srand(time(NULL));
    return (int) rand()%2;
}
  • Reading the documentation for the functions that you call is a requirement in programming. Guessing does not work. – stark Apr 09 '22 at 11:46
  • Normally you only *seed* once. So only `main()` would call `srand()`. There is no fully explicit specification of `srand()`, which is a legacy thing. But in the most common implementations, if you don't have enough duration between two successive calls to `srand(time(NULL))`, the second call will just reset the generator to the same initial state as before, because the count of *seconds* since the Epoch is the same. Hence causing the same “random” number to be generated several times over and over. – jpmarinier Apr 09 '22 at 11:54

0 Answers0