1. Pure random number generator
xn+1= (aXn + c) MoD m
Modulus=231-1=int. MaxValue
multiplier=75=16807
C=0
It is possible to repeat after the number of 2^31-2 has been displayed. Hands-on brain: Write a method that uses the above algorithm to generate a random integer of a specified number (for example, 1000).
/**
* Random number generator
*/
public class Suiji
{
private static final int N = 200;
private static final int left = 40;
private static final int right = 10000;
private static long x0 = 1L;
Private long a = 1103515245L;
Private long C = 12345L;
Private long m = 2147483648L;
Generate random numbers
Private Long rand (long R)
{
A,c,m is constant
R = (R * A + C)% m;//xn+1= (aXn + c) MoD m
return R;
}
/**
* Represents a random number between a~b
*
* @param a
* @param b
* @param Rand
* @return
*/
Private long Little (int a, int b, long rand)
{
Return a + rand% (b-a + 1);
}
private void recursion (int count, long rand)
{
if (Count >= N)
{
Return
}
Rand = rand (RAND);
Long r = Little (left, right, Rand);
System.out.print (R + "");
Recursion (++count, Rand);
}
public static void Main (string[] args)
{
Suiji recur = new Suiji ();
Recur.recursion (0, x0);
}
}
2. Take a look at the following code, do you find anything special?
2
The difference is that the type of the method is different, and the corresponding method is called according to the type of the parameter.
New Java Essays