Generate random numbers (random number generators) without using the Math.random method _java

Source: Internet
Author: User
Tags getmessage pow

Due to some uncontrollable factors, such as system memory, computer status, and so on, each time in the while loop to perform a certain number of times there will be a difference
About hundreds of times. This leads to a difference in results.
Note that this program uses a number of static variables, that is, when the next thread continues to execute the same run method as the previous thread, its initial value is the value of the previous thread's execution, which forms the classic butterfly effect by amplifying the difference, resulting in the final random number.
In this program, a total of 13 thread threads are opened, and each time the values of those static variables are driven in a confusing direction,
So the end gets the array double[] BB's chaotic degree geometry rises,
The first bb[0] has only about hundreds of possible values, and to bb[3] can be any of the 65,536 data.
To be random, I cycled 13 times, bb[12] almost absolutely random.

Copy 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 () {
Deposit 0-65536 of these 65,536 numbers 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) {
Move to 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 fixed point to the effect of the program, when two values of the same description program may enter a dead end, that is, fixed point, on the fixed point of the problem can refer to advanced mathematics on the function of knowledge
}
}
Double ran=dd[12];
return ran;
}

public static void Main (string[] args) {
Double Rs=getrandom_0_1 ();
System.out.println (RS);
}
}

Myrandom.java

Copy 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 () {
Deposit 0-65536 of these 65,536 numbers 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 fixed point to the effect of the program, when two values of the same description program may enter a dead end, that is, fixed point, on the fixed point of the problem can refer to advanced mathematics on the function of knowledge
}
}
Double ran=dd[12];
return ran;
}

public static void Main (string[] args) {
Double Rs=getrandom_0_1 ();
System.out.println (RS);
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.