In Avengers 4, a snap of Thanos’s finger can randomly kill half of the life, and the Thanos script on Github can randomly delete half of the server files. I wonder how to delete half of the files at random? How is the bottom of the random algorithm implemented? I took time to sort out the answers these two days.

Script ontology of Thanos

let "i=`find . -type f | wc -l`/2";
if [[ uname=="Darwin"]].then
    find . -not -name "Thanos.sh" -type f -print0 | gshuf -z -n $i | xargs -0  -- cat;
else
    find . -not -name "Thanos.sh" -type f -print0 | shuf -z -n $i | xargs -0  -- cat;
fi
Copy the code

To fully understand the script, I looked up the keywords let, find, shuf/gshuf, and xargs.

Roughly explain, let I = “` find. -type f | wc -l ` / 2”. Assign I to half the number of lines in the server’s normal file.

If [[uname==”Darwin”]] should be used to determine whether the system is Mac or Linux.

find . -not -name “Thanos.sh” -type f -print0 | gshuf -z -n $i | xargs -0 — cat; Find all ordinary files whose name is not thanos. sh, select $I at random, and run cat operation on these as parameters. If you really delete them, change cat to rm.

Java random algorithm

Time stamp modulus

For example, get a random number in the range [0,100].

final long l = System.currentTimeMillis();
final int i = (int) (l % 100);
Copy the code

Math.random()

For example, get the random number of int.

final double d = Math.random();
final int i = (int) (d*100);
Copy the code

The Random class

I looked at the source code for Math.random(), which also calls methods of the random class.

    / / Math. The random () implementation
    public static double random(a) {
        return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
    }
    
    / / RandomNumberGeneratorHolder is actually the Random class
    private static final class RandomNumberGeneratorHolder {
        static final Random randomNumberGenerator = new Random();
    }
    
    // Implementation of the nextDouble() method in the Random class
        public double nextDouble(a) {
        return (((long)(next(26)) < <27) + next(27)) * DOUBLE_UNIT;
    }
Copy the code

The Random class provides multiple Random methods that return different types and provide two constructors.

    public Random(a) {
        this(seedUniquifier() ^ System.nanoTime());
    }
    
    public Random(long seed) {
        if (getClass() == Random.class)
            this.seed = new AtomicLong(initialScramble(seed));
        else {
            // subclass might have overriden setSeed
            this.seed = newAtomicLong(); setSeed(seed); }}Copy the code

Method, look at the name of the method to see what it does.

Source code is very difficult to see, online to find information that Random Random algorithm has the following characteristics:

  1. Pseudo random.
  2. The same seed is called the same number of times and the result is the same
  3. It’s evenly distributed. Every number in the interval has the same probability.

The algorithm uses linear congruent equations. Next time I finish learning to sort out the materials, to share.

True or false random number

Random number algorithms are pseudorandom, and from the moment the parameter is passed in, the subsequent output is specific. So far this guy is generating true random numbers.

Tortoise tortoise, quantum mechanics. I only see a little fur, when the extracurricular knowledge expansion.

Emmm, if you are interested, I recommend Mr. Wang Jie’s class. I think he is good at physics, including relativity, astronomy, aliens and so on


Welcome to follow the wechat public account, to provide insights and technical articles, occasionally lucky draw, look forward to progress with you. Search for Xiaobing Zhang Jian on wechat or scan the qr code below.