import java.io.*;
import java.util.*;
class randomuni
{
public static void main(String args[])throws IOException
{ int i,j;
int r[]=new int[100];
int arr1[]=new int[100];
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 < 100; 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<100;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.kolmogorav 2.chi square ");
i=Integer.parseInt(b.readLine());
switch(i)
{
case 1:double dplus,dminus,dplmax=0,dmimax=0,d,cv=0.410;
for(i=0;i<99;i++)
{ for(j=i+1;j<100;j++)
{ if(r[i]>r[j])
{ int temp=r[i];
r[i]=r[j];
r[j]=temp;
}
}
}
for(i=0;i<100;i++)
{ j=i+1;
dplus=j/100-r[i];
dminus=r[i]-((j-1)/100);
if(i==0)
{dplmax=dplus;
dmimax=dminus;
}
else
{if(dplus>dplmax)
dplmax=dplus;
if(dminus>dmimax)
dmimax=dminus;
}
}
System.out.println("d+max= "+dplmax+"\nd-max= "+dmimax) ;
if(dmimax>dplmax)
d=dmimax;
else
d=dplmax;
System.out.println("d= "+d) ;
if(d<=cv)
System.out.println("\nRandom no. uniformly distributed ") ;
else
System.out.println("\nRandom no. not uniformly distributed ") ;
break;
case 2:float o[]=new float[100];
double x=0,e=10,cv1=25.2;
for(i=0;i<100;i++)
{ if(r[i]<10)
o[0]++;
else if(r[i]<20)
o[1]++;
else if(r[i]<30)
o[2]++;
else if(r[i]<40)
o[3]++;
else if(r[i]<50)
o[4]++;
else if(r[i]<60)
o[5]++;
else if(r[i]<70)
o[6]++;
else if(r[i]<80)
o[7]++;
else if(r[i]<90)
o[8]++;
else if(r[i]<100)
o[9]++;
}
for(i=0;i<100;i++)
{ x+=(o[i]-e)/e ;}
if(x<=cv1)
System.out.println("\nRandom no. uniformly distributed ") ;
else
System.out.println("\nRandom no. not uniformly distributed ") ;
break;
}
}
}
/*output
Z:\>javac randomuni.java
Z:\>java randomuni
Random number generated using in built function
9 30 48 89 33 92 3 7 55 29 17 56 48 42 65 65 38 97 34 72 64 8 43 43 68 57 10 5 2
2 64 76 26 29 61 72 30 91 42 64 55 46 77 61 36 62 55 41 86 53 96 92 67 15 19 96
90 68 35 67 46 72 72 48 79 58 11 69 18 12 28 43 42 18 10 23 55 97 66 98 72 52 94
0 42 15 5 19 3 57 87 11 85 0 67 46 90 51 69 70 14
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 37 81 53 89 57 41 33 29 77 1 13 69 97 61 93 9 17 21
73 49 37 81 53 89 57 41 33 29 77 1 13 69 97 61 93 9 17 21 73 49 37 81 53 89 57
41 33 29 77 1 13 69 97 61 93 9 17 21 73 49 37 81 53 89 57 41 33 29 77 1 13 69 97
61 93 9 17 21 73 49 37 81 53 89 57 41 33 29 77 1
Select any method to test uniformity--1.kolmogorav 2.chi square
1
d+max= -1.0
d-max= 97.0
d= 97.0
Random no. not uniformly distributed
Z:\>javac randomuni.java
Z:\>java randomuni
Random number generated using in built function
7 42 54 1 95 77 77 10 34 42 51 45 17 33 0 64 24 73 19 72 0 3 50 80 8 3 87 58 83
23 98 78 37 41 6 3 95 62 99 84 86 57 87 88 80 68 4 50 69 55 38 92 49 64 74 95 80
12 2 25 16 65 42 7 95 55 92 74 96 92 36 56 51 48 62 55 97 90 36 43 42 91 97 94
58 69 68 26 28 63 31 2 98 44 84 85 23 37 73 20
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 37 81 53 89 57 41 33 29 77 1 13 69 97 61 93 9 17 21
73 49 37 81 53 89 57 41 33 29 77 1 13 69 97 61 93 9 17 21 73 49 37 81 53 89 57
41 33 29 77 1 13 69 97 61 93 9 17 21 73 49 37 81 53 89 57 41 33 29 77 1 13 69 97
61 93 9 17 21 73 49 37 81 53 89 57 41 33 29 77 1
Select any method to test uniformity--1.kolmogorav 2.chi square
2
Random no. uniformly distributed
*/
PROGRAM TO TEST UNIFORMITY OF RANDOM NO in JAVA
About author: kiran
I am Freelance Facebook Application, Web and Flash Developer based in Mumbai, India. I have also worked on various other Platform like Twitter, Google, YouTube, LinkedIn etc. Apart from web and social app I also develop mobile apps and flash games.
I started this blog when I was Engineering to help some of my friends suffering from coding in labs. Later on Many more students love this blog and received many suggestion to improve it.
Subscribe to:
Post Comments
(
Atom
)

0 comments :