Due to the influence of some uncontrollable factors, such as system memory, computer status, etc., the number of executions in the while loop will be different each time.
Probably hundreds of times. This leads to differences in results.
Note that this program uses many static variables. That is to say, when the next thread continues to execute the same run method as the previous thread, its initial value is the value after the execution of the previous thread. This forms a classic butterfly effect. This difference is amplified, leading to the final random number generation.
In this program, a total of 13 threads are opened, and each time the values of those static variables are pushed in a chaotic direction.
Therefore, the degree of confusion of the final array double[] bb increases geometrically.
The initial bb[0] only has about a few hundred possible values, and when it comes to bb[3] it can be any one of 65536 data.
In order to achieve randomness, I looped 13 times, and bb[12] can be said to be almost absolutely random.
Copy the code code as follows:
/**
* Author:Yuanhonglong
* Date:2014-1-9
*/
public class MyRandom implements Runnable{
private static int random;
private static int f=127;
private static int m=(int)Math.pow(2,16);
private static int[] r=getR();
private static int x=13;
@Override
public void run(){
for(;!Thread.interrupted();){
f=((f/2)+r[f])%m;
random=r[f];
}
}
private static int[] getR(){
//Save the 65536 numbers 0-65536 into r[] in a certain order
int[] r=new int[m];
r[0]=13849;
for(int i=1;i<m;i++){
r[i]=((2053*r[i-1])+13849)%m;
}
int k=r[65535];
r[65535]=r[(f+1)%m];
r[(f+1)%m]=k;
return r;
}
private static void changeR(int[] r,int f){
//Move r[]
int[] r1=new int[r.length];
System.arraycopy(r,0,r1,0,r.length);
for(int i=0;i<r.length;i++){
r[i]=r1[(i+f)%m];
}
}
public static double getRandom_0_1(){
double[] dd=new double[13];
for(int i=0;i<dd.length;i++){
Runnable runnable=new MyRandom();
Thread thread=new Thread(runnable);
thread.start();
try{
Thread.sleep(x+1);
}
catch(InterruptedException e){
e.getMessage();
}
thread.interrupt();
double rr=(double)random/(double)m;
x=f%13;
changeR(r,11+(f/7));
dd[i]=rr;
if((i>0)&&(dd[i]==dd[i-1])){
changeR(r,13+(f/11));
//Prevent the impact of fixed points on the program. When the two values are the same, it means that the program may have entered a dead end, that is, a fixed point. For questions about fixed points, you can refer to the knowledge of functions in advanced mathematics.
}
}
double ran=dd[12];
return ran;
}
public static void main(String[] args){
double rs=getRandom_0_1();
System.out.println(rs);
}
}
MyRandom.java
Copy the code code as follows:
/**
* Author:Yuanhonglong
* Date:2014-1-9
*/
package mine.loop;
public class MyRandom implements Runnable{
private static int random;
private static int f=127;
private static int m=(int)Math.pow(2,16);
private static int[] r=getR();
private static int x=13;
@Override
public void run(){
for(;!Thread.interrupted();){
f=((f/2)+r[f])%m;
random=r[f];
}
}
private static int[] getR(){
// Store the 65536 numbers 0-65536 in r[] in a certain order
int[] r=new int[m];
r[0]=13849;
for(int i=1;i<m;i++){
r[i]=((2053*r[i-1])+13849)%m;
}
int k=r[65535];
r[65535]=r[(f+1)%m];
r[(f+1)%m]=k;
return r;
}
private static void changeR(int[] r,int f){
int[] r1=new int[r.length];
System.arraycopy(r,0,r1,0,r.length);
for(int i=0;i<r.length;i++){
r[i]=r1[(i+f)%m];
}
}
public static double getRandom_0_1(){
double[] dd=new double[13];
for(int i=0;i<dd.length;i++){
Runnable runnable=new MyRandom();
Thread thread=new Thread(runnable);
thread.start();
try{
Thread.sleep(x+1);
}
catch(InterruptedException e){
e.getMessage();
}
thread.interrupt();
double rr=(double)random/(double)m;
x=f%13;
changeR(r,11+(f/7));
dd[i]=rr;
if((i>0)&&(dd[i]==dd[i-1])){
changeR(r,13+(f/11));
// Prevent the impact of fixed points on the program. When the two values are the same, it means that the program may have entered a dead end, that is, a fixed point. For questions about fixed points, you can refer to the knowledge of functions in advanced mathematics.
}
}
double ran=dd[12];
return ran;
}
public static void main(String[] args){
double rs=getRandom_0_1();
System.out.println(rs);
}
}