Random algorithm

Pseudorandom: For example, a pseudorandom number is a seemingly random sequence calculated using a deterministic algorithm, so a pseudorandom number is not actually random. If the starting value used in the calculation of pseudorandom number is unchanged, then the number order of the pseudorandom number is also unchanged. The randomness of a pseudorandom number can be measured by its statistical properties, the main characteristics of which are the probability of each number appearing and its relationship to other numbers in the sequence. The advantage of pseudo-random number is that it is relatively simple to calculate, and it is difficult to calculate the algorithm using only a few values. Generally people use a pseudo-random number, such as the time on the computer, as a starting point for calculating the pseudo-random number.

Cloud.tencent.com/developer/a…

All the algorithms I’ve talked about are deterministic, that is, the same input always corresponds to the same output. But in practice, uncertain algorithms are often used, such as random number generation algorithm **, the result of the algorithm is uncertain, we call this algorithm (random) probability algorithm, ** is divided into the following four categories:

1. Numerical probability algorithm

Used to solve numerical problems, usually approximate solutions

2. Monte Carlo algorithm

Can get a solution of the problem, but not necessarily the correct solution, the probability of correct depends on the algorithm running time, the more time the algorithm used, the higher the probability of correct. To find the exact solution of the problem;

3. Las Vegas

Call random algorithms until the correct solution is found or the number of calls reaches a certain threshold. So, if you can get a solution, it must be the right solution.

4. Sherwood algorithm

The random algorithm is used to modify the existing algorithm, so that the performance of the algorithm is independent of the input data as far as possible, that is, the performance of the smoothing algorithm. It always finds a solution to a problem, and it always finds the right solution.

The random number

An overview of the

Random numbers generated by computers are pseudorandom numbers obtained by linear congruence method.

Method: Generate random sequence

D is called seed; The greater the value of m, the better; M and B are mutually prime, and b is often taken as prime.

Monte Carlo method (Monte Carlo Method), also known as statistical simulation method, is in the mid-1940s due to the development of science and technology and the invention of electronic computers, And a kind of very important numerical calculation method guided by probability and statistics theory is proposed. A method of using random numbers (or, more commonly, pseudo-random numbers) to solve many computational problems. And the corresponding is deterministic algorithms. Monte Carlo method is widely used in financial engineering, macroeconomics, computational physics (such as particle transport calculation, quantum thermodynamics calculation, aerodynamics calculation) and other fields. A number related to PI can be found by measuring the area of a quarter of a circle by the area of a square.

/ * * *@author SJ
 * @date2020/11/6 * /
public class CalculatePI {
    public static double calculatePi(int num){
        int shoot=0;
        for (int i = 0; i < num; i++) {
            double x = Math.random();
            double y = Math.random();
            if ((x*x+y*y)<=1){ shoot++; }}return (double) 4*shoot/num;
    }
    public static void main(String[] args) {
        double v = calculatePi(500);
        System.out.println(v);
        double v1 = calculatePi(5000);
        System.out.println(v1);
        double v2 = calculatePi(50000);
        System.out.println(v2);
        double v3 = calculatePi(500000); System.out.println(v3); }}Copy the code

Results:

"C: \ Program Files \ Java \ jdk1.8.0 _131 \ bin \ Java exe".3.08
3.104
3.13264
3.139048

Process finished with exit code 0

Copy the code

A method whose return type is Boolean is automatically highlighted by IDEA, indicating that Boolean method ‘XXX’ is always inverted. And provide a solution of Invert method.

Your method always returns a value that is not (!). Use, why not just return a value after the value is not.