Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
269 views

LCG Algorithm

This document discusses a C++ program that implements a linear congruential generator to generate random numbers. The program takes in a seed value, uses the linear congruential generator formula to generate 100 random numbers between 0 and 1, bins the numbers into groups of 0.1 intervals, and performs a chi-squared test to determine if the random numbers are uniformly distributed or correlated. The output shows the generated random numbers and the number of values in each bin, and indicates that the random numbers pass the independence test.

Uploaded by

Ninet Nabil
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
269 views

LCG Algorithm

This document discusses a C++ program that implements a linear congruential generator to generate random numbers. The program takes in a seed value, uses the linear congruential generator formula to generate 100 random numbers between 0 and 1, bins the numbers into groups of 0.1 intervals, and performs a chi-squared test to determine if the random numbers are uniformly distributed or correlated. The output shows the generated random numbers and the number of values in each bin, and indicates that the random numbers pass the independence test.

Uploaded by

Ninet Nabil
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Ain Shams University

Faculty of Engineering
Computers and Systems Department

Linear Congruential
Generator
o The code
#include "stdafx.h"
#include "iostream"
#include <fstream>
#define a 21
#define m 1000
#define c 71
#define N 10
double alpha05[11]={0,3.84,5.99,7.81,9.49,11.07,12.53,14.07,15.51,16.93,20.48};
using namespace std;
void generator (int seed,int * rn)
{
int i=0;
do
{
rn[i]=((a*seed)+c)% m;
seed=rn[i++];
}
while(i<100);
}
int _tmain(int argc, _TCHAR* argv[])
{
int o[10]={0};
int seed=1;
int r[100];
float arr[100];
double chi=0.0;
generator(seed,r);
ofstream out2("out2.txt");

for(int i=0;i<100;i++)
{
arr[i]=((float)r[i]/1000);
if ((arr[i]>0.0)&&(arr[i]<0.1))
{
o[0]++;
}
else if ((arr[i]>0.1)&&(arr[i]<0.2))
{
o[1]++;
}
else if ((arr[i]>0.2)&&(arr[i]<0.3))
{
o[2]++;
}
else if ((arr[i]>0.3)&&(arr[i]<0.4))
{
o[3]++;
}
else if ((arr[i]>0.4)&&(arr[i]<0.5))
{
o[4]++;
}
else if ((arr[i]>0.5)&&(arr[i]<0.6))
{
o[5]++;
}
else if ((arr[i]>0.6)&&(arr[i]<0.7))
{
o[6]++;
}
else if ((arr[i]>0.7)&&(arr[i]<0.8))
{
o[7]++;
}
else if ((arr[i]>0.8)&&(arr[i]<0.9))
{
o[8]++;
}
else
{
o[9]++;
}
}
int E=10;
for(int t=0;t<100;t++)
{
out2<<arr[t]<<" ";
}
for(int p=0;p<10;p++)
{
printf(" observation for class %d = %d\n",p,o[p]);
}

for(int w=0;w<10;w++)
{
chi+=(((o[w]-E)*(o[w]-E))/(float)E);
}
if(chi < alpha05[N-1])
{
cout<<"\nTEST SUCCEED\n";
cout<<"\nthese Random Numbers are low correlated"<<endl;
}
else
{
cout<<"\nTEST FAILED\n";
cout<<"\nthese Random Numbers are correlated"<<endl;
}

system("pause");
return 0;
}

o Output Random Numbers:


0.092 0.003 0.134 0.885 0.656 0.847 0.858 0.089 0.94 0.811
0.102 0.213 0.544 0.495 0.466 0.857 0.068 0.499 0.55 0.621
0.112 0.423 0.954 0.105 0.276 0.867 0.278 0.909 0.16 0.431
0.122 0.633 0.364 0.715 0.086 0.877 0.488 0.319 0.77 0.241
0.132 0.843 0.774 0.325 0.896 0.887 0.698 0.729 0.38 0.051
0.142 0.053 0.184 0.935 0.706 0.897 0.908 0.139 0.99 0.861
0.152 0.263 0.594 0.545 0.516 0.907 0.118 0.549 0.6 0.671
0.162 0.473 0.004 0.155 0.326 0.917 0.328 0.959 0.21 0.481
0.172 0.683 0.414 0.765 0.136 0.927 0.538 0.369 0.82 0.291
0.182 0.893 0.824 0.375 0.946 0.937 0.748 0.779 0.43 0.101

o Output of the program

You might also like