The math.random method is not used to generate a random number generator of random Numbers

  • 2020-04-01 02:45:28
  • OfStack

Due to some uncontrollable factors, such as system memory, computer state, etc., the number of executions in the while loop will vary
Hundreds of times. This leads to differences in results.
Note that this program USES a lot of static variables, that is, when the next thread continues to execute the same run method as the previous thread, the initial value is the value after the previous thread's execution, which forms the classic butterfly effect, resulting in the final random number by magnifying the difference.
In this program, a total of 13 threads are started, each time pushing the values of those static variables in a chaotic direction,
So the resulting array double[] bb goes up in clutter geometrically,
The original bb[0] has only a few hundred possible values, but by bb[3] it can be any one of 65,536 data sets.
In order to be random, I cycled 13 times, and bb[12] was almost absolutely random.



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(){
                //Put 0-65536 Numbers 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));
                                //To prevent the influence of fixed point on the program, when two values are the same, the program may enter a dead end, that is, the fixed point, the problem of fixed point can refer to the knowledge of higher mathematics about the function
   }
  }
  double ran=dd[12];
  return ran;
 }

 public static void main(String[] args){
  double rs=getRandom_0_1();
  System.out.println(rs);
 }
}

MyRandom. Java



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(){
  //Put 0-65536 Numbers 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){
  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));
    //To prevent the influence of fixed point on the program, when two values are the same, the program may enter a dead end, that is, the fixed point, the problem of fixed point can refer to the knowledge of higher mathematics about the function
   }
  }
  double ran=dd[12];
  return ran;
 }

 public static void main(String[] args){
  double rs=getRandom_0_1();
  System.out.println(rs);
 }
}


Related articles: