C/Random number always the same.
Expert: Zlatko - 1/6/2012
QuestionHi again,
For some reason I couldn't follow up with the previous question so I had to make a new one. I'm not sure if I understood you correctly but I tried what you suggested. I got the correct figures in the text file however the problem regarding the random number is that it is always the same, yet again.
Below is the coding:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define LINE_1 "Total Number of Bees: %d\n"
#define LINE_2 "Total Number of Infected Bees: %d\n"
#define LINE_3 "Total Amount of Money: %f\n"
#define LINE_4 "Total Amount of Honey Jars: %d\n"
#define LINE_5 "Total Amount of Beeswax: %d\n"
#define FILENAME "Inventory.txt"
int main ()
{
int Inv_NumofBees;
int Inv_InfectedBees;
float Inv_Money;
int Inv_HoneyJars;
int Inv_Wax;
int infected = rand()<=Inv_NumofBees;
FILE* file;
file = fopen(FILENAME, "r"); /*Opens the file*/
if (file == NULL)
{
printf("Cannot open file %s for reading\n", FILENAME);
getchar();
getchar();
return 0;
}
if (
fscanf(file, LINE_1, &Inv_NumofBees) != 1
||
fscanf(file, LINE_2, &Inv_InfectedBees) != 1
||
fscanf(file, LINE_3, &Inv_Money) != 1
||
fscanf(file, LINE_4, &Inv_HoneyJars) != 1
||
fscanf(file, LINE_5, &Inv_Wax) != 1
)
{
printf("Error reading numbers.\n");
getchar();
getchar();
return 0;
}
srand (time(NULL));
srand(1);
printf ("The number of bees that have been infected is %d.\n", infected);
fclose(file);
file = fopen(FILENAME, "w");
if (file == NULL)
{
printf("Cannot open file %s for writing\n", FILENAME);
return 0;
}
fprintf(file, LINE_1, Inv_NumofBees-infected);
fprintf(file, LINE_2, Inv_InfectedBees+infected);
fprintf(file, LINE_3, Inv_Money);
fprintf(file, LINE_4, Inv_HoneyJars);
fprintf(file, LINE_5, Inv_Wax);
fclose(file);
getchar();
getchar();
return 0;
}
AnswerHi Stephane
You see the same value for infected because of this statement:
infected = rand()<=Inv_NumofBees
The expression:
rand()<=Inv_NumofBees
is a comparison, and a comparison is either true or false. True gives the value of 1, and false gives 0. These are the only 2 values you would get into infected with the code you have.
Here is what you should do
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define LINE_1 "Total Number of Bees: %d\n"
#define LINE_2 "Total Number of Infected Bees: %d\n"
#define LINE_3 "Total Amount of Money: %f\n"
#define LINE_4 "Total Amount of Honey Jars: %d\n"
#define LINE_5 "Total Amount of Beeswax: %d\n"
#define FILENAME "Inventory.txt"
int main ()
{
int Inv_NumofBees;
int Inv_InfectedBees;
float Inv_Money;
int Inv_HoneyJars;
int Inv_Wax;
FILE* file;
int infected;
/* seed the random number generator **before** it is used */
srand(time(NULL));
file = fopen(FILENAME, "r"); /*Opens the file*/
if (file == NULL)
{
printf("Cannot open file %s for reading\n", FILENAME);
getchar();
getchar();
return 0;
}
if (
fscanf(file, LINE_1, &Inv_NumofBees) != 1
||
fscanf(file, LINE_2, &Inv_InfectedBees) != 1
||
fscanf(file, LINE_3, &Inv_Money) != 1
||
fscanf(file, LINE_4, &Inv_HoneyJars) != 1
||
fscanf(file, LINE_5, &Inv_Wax) != 1
)
{
printf("Error reading numbers.\n");
getchar();
getchar();
return 0;
}
fclose(file);
/* get an infected value which is between 0 and
the number of bees which you have.
Of course you need to know how many bees you have first,
so we put this **after** the file reading
By using the modulo operator, you can be sure that
the infected value will be less than or equal to
the number of bees you have.
For example, if you have 3 bees you will do modulo 4 (always 1 more),
so that infected can be 0,1,2 or 3
*/
infected = rand() % (Inv_NumofBees + 1);
printf ("The number of bees that have been infected is %d.\n", infected);
file = fopen(FILENAME, "w");
if (file == NULL)
{
printf("Cannot open file %s for writing\n", FILENAME);
return 0;
}
fprintf(file, LINE_1, Inv_NumofBees-infected);
fprintf(file, LINE_2, Inv_InfectedBees+infected);
fprintf(file, LINE_3, Inv_Money);
fprintf(file, LINE_4, Inv_HoneyJars);
fprintf(file, LINE_5, Inv_Wax);
fclose(file);
getchar();
getchar();
return 0;
}