灭霸脚本怎么随机删除服务器的一半文件?

2,551

复联4中灭霸一个响指可以随机消灭一半生命,Github 上的灭霸脚本可以随机删除一半服务器文件。我在想怎么做到随机删除一半的文件呢??随机算法的底层怎么实现的?这两天抽时间整理了答案。

灭霸脚本本体

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

为了彻底读懂这段脚本,特意查了几个关键字letfindshuf/gshufxargs

大致解释下,let "i=`find . -type f | wc -l`/2"; 将i赋值为服务器普通文件行数的一半。

if [[ uname=="Darwin" ]] 应该是用来判断系统是 Mac or Linux 的,Linux 用 shuf 做随机,Mac 用 gshuf

find . -not -name "Thanos.sh" -type f -print0 | gshuf -z -n $i | xargs -0 -- cat; 找到所有名称不是 Thanos.sh 的普通文件,随机选取 $i 个,并将这些作为参数执行 cat 操作,真正删除将 cat 改为 rm 即可。

Java 随机算法

时间戳取模

例如获取 [0,100) 区间的随机数。

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

Math.random()

例如获取 int 的随机数。

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

Random 类

我看了 Math.random() 的源码,也是调用 Random 类的方法。

    // Math.random() 实现
    public static double random() {
        return RandomNumberGeneratorHolder.randomNumberGenerator.nextDouble();
    }
    
    // RandomNumberGeneratorHolder 其实是 Random 类
    private static final class RandomNumberGeneratorHolder {
        static final Random randomNumberGenerator = new Random();
    }
    
    // nextDouble() 方法在 Random 类中的实现
        public double nextDouble() {
        return (((long)(next(26)) << 27) + next(27)) * DOUBLE_UNIT;
    }

Random 类提供贼多随机方法,返回的类型不同,提供两个构造器。

    public Random() {
        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 = new AtomicLong();
            setSeed(seed);
        }
    }

方法的话,看一下方法名就知道干啥了的吧。

源码看得挺困难,网上找的资料表示 Random 的随机算法有一下特点:

  1. 伪随机。
  2. 相同种子的相同次数调用结果相同
  3. 均匀分布,区间内每个数字概率相同。

算法用到了线性同余方程。下期我学完后整理下资料,分享出来。

真伪随机数

随机数算法都是伪随机数,从参数传入的那一刻,以后的输出是特定的了。目前产生真随机数的是这个东东。

龟龟,用到量子力学了。我也只看了点皮毛,当课外知识扩展吧。

emmm,有兴趣的推荐看汪洁老师讲的课,我觉得他对物理这块科普的挺好的,还包括相对论、天文、外星人等等


欢迎关注微信公众号,提供感悟和技术文章,偶尔抽奖,期待和你一起进步。微信搜索小兵张健或扫描以下二维码。