import java.util.*;
class randomind
{
public static void main(String args[])throws IOException
{ int i,j;
int r[]=new int[10];
double u,var2,var,z,cv=1.96,n1=0,n2=0,mean=49.5;
int run[]=new int[10];
int ar=0;
int arr1[]=new int[10];
BufferedReader b=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Random number generated using in built function ");
Random randomGenerator = new Random();
for (i = 0; i < 10; i++)
{
int randomInt = randomGenerator.nextInt(100);
arr1[i]=randomInt;
System.out.print(randomInt+" ");
}
System.out.println("\nRandom number generated using linear congruential method ");
System.out.println("Enter the values of X0,a,c and m \n");
int xo=Integer.parseInt(b.readLine());
int a=Integer.parseInt(b.readLine());
int c=Integer.parseInt(b.readLine());
int m=Integer.parseInt(b.readLine());
for(i=0;i<10;i++)
{
int rand=((a*xo+c)%m);
r[i]=rand;
System.out.print(r[i]+" ");
xo=rand;
}
System.out.println("\nSelect any method to test uniformity--1.runs up & down 2.runs above & below mean ");
i=Integer.parseInt(b.readLine());
switch(i)
{
case 1:
for(i=0;i<9;i++)
{ if(r[i]
run[i]=1;
else
run[i]=0;
if(i==0)
ar=1;
else
{ if(run[i]!=run[i-1])
ar++;
}
}
u=((2*10)-1)/3;
var2=((16*10)-29)/90;
var=Math.sqrt(var2);
z=(ar-u)/var;
System.out.println("z "+z) ;
if(z<=cv)
System.out.println("\nRandom no. independent ") ;
else
System.out.println("\nRandom no. not independent ") ;
break;
case 2:
for(i=0;i<10;i++)
{ if(r[i]
run[i]=0;
else
run[i]=1;
if(run[i]==1)
n1++;
else
n2++;
}
for(i=0;i<9;i++)
{
if(i==0)
ar=1;
else
{ if(run[i]!=run[i-1])
ar++;
}
}
u=((2*n1*n2)/10)+0.5;
var2=(2*n1*n2*(2*n1*n2-10))/(10*10*(10-1));
var=Math.sqrt(var2);
z=(ar-u)/var;
System.out.println("z "+z) ;
if(z<=cv)
System.out.println("\nRandom no. independent ") ;
else
System.out.println("\nRandom no. not independent ") ;
break;
}
}
}
/*output
Z:\>javac randomind.java
Z:\>java randomind
Random number generated using in built function
84 92 90 44 37 32 13 11 42 70
Random number generated using linear congruential method
Enter the values of X0,a,c and m
1
13
0
100
13 69 97 61 93 9 17 21 73 49
Select any method to test uniformity--1.runs up & down 2.runs above & below mean
1
z 0.0
Random no. independent
Z:\>javac randomind.java
Z:\>java randomind
Random number generated using in built function
9 41 35 9 91 49 61 96 95 40
Random number generated using linear congruential method
Enter the values of X0,a,c and m
1
13
0
100
13 69 97 61 93 9 17 21 73 49
Select any method to test uniformity--1.runs up & down 2.runs above & below mean
2
z -1.0062305898749053
Random no. independent
*/
No comments:
Post a Comment