Statistically perfect random number generator software

Statistically perfect random number generator software



The problem of creating such programs was widely discussed in the mathematical literature in 1965-1975, at the same time the complexity of this problem was noted.







Modern mathematics has made significant advances in this matter.







They are accessible to narrow specialists, but difficult to understand, and have been removed from widespread discussion.







I propose here a simple solution to this problem, it may not be worthy of the attention of great mathematicians, but it is quite accessible to beginners.







I have been able to create a program that generates a sequence of characters '0' and '1' with excellent statistical properties.







The design of the program is simple and easy to understand.







A random sequence of zeros and ones must have the following properties:







  • the number of zeros and ones in all fragments of this sequence is approximately the same;
  • the number of occurrences of fragments 00, 01, 10 and 11 is always approximately the same;
  • the same applies to all kinds of fragments of length 3, 4, 5, and so on.

    It is clear that the program can count the number of occurrences of such fragments in the already formed sequence and generate the next symbol so as to maintain the listed equalities.


If you analyze these equalities in ascending order of the fragment length, the program will quickly loop.







, , .







?







'0' '1' ( ), , .







, .

, ( lenmask).







, lenmask .







.

s0 s1, .







s0 s1 , s0>s1 1, 0 .







s0 s1 , , 0 1 .







, , .







, , , .







JAVA .

.







.







.







.







The number of occurrences of these masks in the generated sequence is counted.

As expected, the control results demonstrate that the number of such occurrences depends on the length of the mask and almost does not depend on its filling.







It is surprising that such a completely elementary program allows one to obtain a result that seemed unattainable by simple means.







application



JAVA program text



//gerase = generator random sequence
//   
//    
package ikojar;
public class gerase {
public static void main(String[] args) {
//  
//,     
//   beg  
//  
int beg=5,
//  
lenrez=2048,
//maxpower      
//     
//    
//   
//   
//   
//     
//      
//   
maxpower=10;

//   
//rs  random symbol,   , 
//       
//     ,  
//   ,       
byte rs=0;
// result     
//  
byte[] result=new byte[lenrez];
// mask    ,  
//   , 
//     
//     , 
//       
byte[] mask=new byte[lenrez];

//   
//   beg
//    beg   
//     result
int p=beg,bp=0;
for(;;)
{//cir raspak
result[bp]=(byte)(p%2);
bp++;
if(p<2)break;
p/=2;
}//cir raspak
//  
String so="  ";
for(int i=0;i<bp;i++)so+=String.valueOf(result[i]);
System.out.println(so);
//  
System.out.println(" ");

//   
for(int pos=bp;pos<lenrez;pos++)
{//circlepos
// hs(hard symbol)  
// ,      
//    
//      , 
//      rs
int hs=0;
//    
//lenmask   
for(int lenmask=pos-2;lenmask>=0;lenmask--)
{//lenmask
//    
//  
for(int i=0;i<lenmask;i++)mask[i]=result[pos+i-lenmask];
//s0  s1    
//     
//      
//   
//      
int s0=0,s1=0;
//     
//   
for(int i=lenmask;i<pos;i++)
{//calcS01
//eqm     
//    
int eqm=1;
// eqm
for(int i1=0;i1<lenmask;i1++)if(mask[i1]!=result[i+i1-lenmask]){eqm=0;break;}
//      ,  
//    
if(eqm==1)if(result[i]==0)s0++;else s1++;
}//calcS01

//     
//     s0  s1
// hs   1,  
//     
if(s0<s1){result[pos]=0;hs=1;break;}
if(s1<s0){result[pos]=1;hs=1;break;}
}//lenmask
if(hs==1)continue;
//     , 
//     rs
result[pos]=rs;rs=(byte)(1-rs);
}//circlepos

//  
//    
so="";
for(int i=0,c=0;i<lenrez;i++)
{//out rez
so+=String.valueOf(result[i]);
c++;
if(c==64){c=0;System.out.println(so);so="";}
}//out rez
System.out.println(so);

//     
System.out.println
("   ");

//     
//pw    , 
//   
for(int pw=1;pw<maxpower;pw++)
{//pw
//    
//    
//       
//     
//   
//      0  pm-1
//  pm      
int pm=1;for(int i=0;i<pw;i++)pm*=2;
//   ,    
//    
System.out.println("   "+String.valueOf(pw));
int mincount=lenrez,maxcount=0;
//      0  pm-1, 
//      
for(int im=0;im<pm;im++)
{//im
//     im
p=im;
for(int i=pw-1;i>=0;i--)
{mask[i]=(byte)(p%2);p/=2;}

//     
//   
// s     
//    
int s=0;
for(int i0=pw;i0<=lenrez;i0++)
{//i0
//      
int eqm=1;
for(int i1=0;i1<pw;i1++)if(result[i0+i1-pw]!=mask[i1]){eqm=0;break;}
if(eqm==1)s++;
}//i0
//    
//   
//     
//System.out.println(String.valueOf(s));

//    
if(s<mincount)mincount=s;
if(s>maxcount)maxcount=s;
}//im
System.out.println(" ="+String.valueOf(mincount));
System.out.println(" ="+String.valueOf(maxcount));
}//pw

return;
}//main

}//class
      
      






All Articles