Middle-square Method Website Links For
Method
 

Information About

Middle-square Method




For example, to generate a sequence of ten-digit pseudorandom numbers, you would create a ten-digit starting value and square it. The middle ten digits of the result would be the next number in the sequence. You would then square that, and so on.

Clearly, for a generator of ''n''-digit numbers, the period can be no longer than 10''n''. If the middle ten digits are all zeroes, the generator then outputs zeroes forever. If the first half of a number in the sequence is zeroes, the subsequent numbers will be decreasing to zero. While these runs of zero are easy to detect, they occur too frequently for this method to be of practical use.

Von Neumann was aware of these problems, but for his purposes the middle-square method was quick (important for use on the ENIAC ), and its errors were easy to detect (when it failed, it generally failed spectacularly).

The following is a simple C++ program that generates the random numbers given an initial remainder we refer to as Zo= Xn in our case above.

#include
void main()
{
int n=0,a,c,m,Z {Link without Title} ;
float U {Link without Title} ;
cout<<"enter z "< cin>>Z {Link without Title} ;
cout<<"enter a "< cin>>a;
cout<<"enter c "< cin>>c;
cout<<"enter m "< cin>>m;
cout<<"n"<<" "<<"Zn"<<" "<<"Un"<<" "< cout<<"..."<<" "<<"..."<<" "<<"..."<<" "< do {

U {Link without Title} =(float)1/(Z {Link without Title} );
else
Z {Link without Title} =0;

cout< n++;
} while(Z {Link without Title} !=Z {Link without Title} );

}


SEE ALSO