R
Red Dragon
I am self study C program student.
Hope someone can help me with this problem.
This program generates random numbers over a user defined range using call function
I used the call function " GenRndNum". The range is 2 and 10.
The problem is that I get the same 2 random numbers generated over 2 calls.
I should get 2 different random numbers. Can someone please point out my mistake?
Thanks
Khoon.
/* Generation of Random Numbers over a User Defined Range.*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int GenRndNum (int x, int y);
int main (void)
{
int MinRange;
int MaxRange;
int RndNum1;
int RndNum2;
printf ("Please key in the Minimum and Maximum Value for the Range of the two ");
printf ("\nrandom numbers> ");
scanf ( "%d %d", &MinRange, &MaxRange);
RndNum1= GenRndNum (MinRange, MaxRange);
printf ("RndNum1 = %d\n",RndNum1);
RndNum2= GenRndNum (MinRange, MaxRange);
printf ("RndNum2 = %d\n",RndNum2);
return 0;
}
int GenRndNum (int x, int y )
{ int z;
srand (time(NULL));
printf ("x = %d, y = %d\n",x,y);
z = rand() % ((y+1 ) - x ) + x;
printf ("z = %d\n",z);
return (z);
}
/*-----------------------
Result of Output:
Please key in the Minimum and Maximum Value for the Range of the two
random numbers> 2 10
x = 2, y = 10
z = 5
RndNum1 = 5
x = 2, y = 10
z = 5
RndNum2 = 5
Press any key to continue_
*/
Hope someone can help me with this problem.
This program generates random numbers over a user defined range using call function
I used the call function " GenRndNum". The range is 2 and 10.
The problem is that I get the same 2 random numbers generated over 2 calls.
I should get 2 different random numbers. Can someone please point out my mistake?
Thanks
Khoon.
/* Generation of Random Numbers over a User Defined Range.*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int GenRndNum (int x, int y);
int main (void)
{
int MinRange;
int MaxRange;
int RndNum1;
int RndNum2;
printf ("Please key in the Minimum and Maximum Value for the Range of the two ");
printf ("\nrandom numbers> ");
scanf ( "%d %d", &MinRange, &MaxRange);
RndNum1= GenRndNum (MinRange, MaxRange);
printf ("RndNum1 = %d\n",RndNum1);
RndNum2= GenRndNum (MinRange, MaxRange);
printf ("RndNum2 = %d\n",RndNum2);
return 0;
}
int GenRndNum (int x, int y )
{ int z;
srand (time(NULL));
printf ("x = %d, y = %d\n",x,y);
z = rand() % ((y+1 ) - x ) + x;
printf ("z = %d\n",z);
return (z);
}
/*-----------------------
Result of Output:
Please key in the Minimum and Maximum Value for the Range of the two
random numbers> 2 10
x = 2, y = 10
z = 5
RndNum1 = 5
x = 2, y = 10
z = 5
RndNum2 = 5
Press any key to continue_
*/