Hands on the brain 1
Full "handwritten code implementation" random number generation
Pure random number generator
Modulus=231-1=int. MaxValue
multiplier=75=16807
C=0
Once the 231-2 number is displayed, it is possible to repeat.
Write a method that uses the above algorithm to generate a random integer of a specified number (for example, 1000).
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;
}
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);
}
}
Hands on the Brain 2
"method overloading"
Take a look at the following code, do you find anything special?
I find that functions of the same name can be different, which makes the function more comprehensive, and the different parameter types can automatically invoke the corresponding function (method), that is, the overloading of the Method.
Two or more methods that meet the following conditions form an "overloaded" relationship:
(1) The method name is the same;
(2) the parameter types are different, the number of parameters is different, or the order of the parameter types is Different.
Note: The return value of the method is not used as a criterion for method overloading.
Java Hands-on Brain IV lecture homework