Simulation and Modelling - Old Questions
12. Write a computer program that will generate three digit random numbers using the linear congruential method. Allow the user to input values of X0, a, c and m.
# include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int x[10],n, a, c, m, i, j;
float r[10];
printf("Enter the number of random number to generate:");
scanf("%d", &n);
printf("Enter the value of seed x0:);
scanf("%d", &x[0]);
printf("Enter the value of a:");
scanf("%d", &a);
printf("Enter the value of c:");
scanf("%d", &c);
printf("Enter the value of m:");
scanf("%d", &m);
i=0;
r[0] = float(x[0]/m);
if (m ==1000)
{
do{
x[i+1] = (a*x[i]+c)%m;
r[i+1] = float(x[i+1]/m);
i++;
}while(i<=n);
printf("The obtained random numbers are:");
for(j=0; j<n; j++)
{
printf("%f", &r[j];
}
else{
printf("To generate three digit random number the value of m should be 1000 !!");
}
return 0;
}