#include<stdio.h>
#include<conio.h>
void print(int x[],float r[],int n,int p)
{
int i;
clrscr();
printf("\n\nLinear Congruential Method!!");
printf("\n\ni\tNumbers\t\tRandom Numbers\n\n");
for(i=0;i<n;i++)
printf("\n%d\t%d\t\t%f",i+1,x[i],(float)r[i]);
printf("\n\nThe total periods=%d",n);
if(i%p==0)
printf("\n\nThe generator achieved the max period!!");
else
printf("\n\nThe generator does not achieved max period!!");
getch();
exit(0);
}
void main()
{
int i,x[40],a,m,x0,ch,p;
float r[40];
clrscr();
printf("\n\n*************Multiplicative Congruential Generator*****************\n");
printf("\n\n1.a=11,m=16,x0=7");
printf("\n\n2.a=11,m=16,x0=8");
printf("\n\n3.a=7,m=16,x0=7");
printf("\n\n4.a=7,m=16,x0=8");
printf("\n\n5.Exit!!");
printf("\n\n********************************************************************\n");
printf("\nEnter your choice=");
scanf("%d",&ch);
switch(ch)
{
case 1:a=11;
m=16;
x0=7;
break;
case 2:a=11;
m=16;
x0=8;
break;
case 3:a=7;
m=16;
x0=7;
break;
case 4:a=7;
m=16;
x0=8;
break;
case 5:exit(0);
default:printf("\n\nYou entered a wrong choice!!");
getch();
exit(0);
}
x[0]=x0;
r[0]=(float)x[0]/m;
for(i=1;i<40;i++)
{
x[i]=(x[i-1]*a)%m;
r[i]=(float)x[i]/m;
if(x[0]==x[i])
{p=m/4;
print(x,r,i,p);
}
}
}
Comments