算法根据先前的结果调整RNG

时间:2016-01-11 13:34:41

标签: algorithm random

我试图为我的模拟器重现一些行为。

我的百分比检查可以在30%到70%之间变化。事情是它不是严格随机的。这是我提取的实时数据:

https://docs.google.com/spreadsheets/d/1JLXRxh7xvR_fYwnTtvQ72jNV0v4lHiEEY6o-kG52wQA/pubhtml

我从数据中注意到的一些事情:

  • 如果检查失败,则下一次检查百分比会增加
  • 70%的检查连续两次失败。
  • 所有第一轮似乎都有较低的成功率20%
  • 生成数据的服务器使用PHP,因此可能使用已知为伪随机的123函数

所以我想在Java中重现这种行为。我可以编写我自己的跟随这些模式的随机类,但我不确定我的实时数据是否涵盖了所有情况,所以我想知道:现有的PRNG算法是否符合这种行为?另外,我将如何调整机会以使全球比率保持在原始值附近?

1 个答案:

答案 0 :(得分:1)

这是如此简单和具体,不太可能,有人已经发布了这个。成为第一个将是一种荣幸。

import java.util.Random;
public class FailGenerator {
    private double firstChance;
    private double nextChance;
    private boolean first;
    private boolean lastResult;
    private Random r;

    public FailGenerator(){
        this(0.5, 0.7);
    }
    public FailGenerator(double firstChance, double nextChance){
        this.firstChance = firstChance;
        this.nextChance = nextChance;
        first = true;
        lastResult = true;
        r = new Random();
    }

    public boolean didHeSucceed(){
        if (lastResult == false){ //if he failed before
            lastResult = true;
        } else {
            double chance;
            if (first){
                first = false;
                chance = firstChance;
            } else {
                chance = nextChance;
            }

            if (r.nextDouble() <= chance){
                lastResult = true;
            } else {
                lastResult = false;
            }
        }

        return lastResult;
    }
}

然后如果你这样称呼它:

public static void main(String[] args) {
    FailGenerator gen = new FailGenerator();
    for (int i = 0; i < 50; i++) {
        System.out.println("The result for iteration no. " + i + " is " + gen.didHeSucceed());
    }
}

结果如下:

The result for iteration no. 0 is false
The result for iteration no. 1 is true
The result for iteration no. 2 is false
The result for iteration no. 3 is true
The result for iteration no. 4 is true
The result for iteration no. 5 is true
The result for iteration no. 6 is true
The result for iteration no. 7 is true
The result for iteration no. 8 is true
The result for iteration no. 9 is false
The result for iteration no. 10 is true
The result for iteration no. 11 is true
The result for iteration no. 12 is true
The result for iteration no. 13 is true
The result for iteration no. 14 is true
The result for iteration no. 15 is false
The result for iteration no. 16 is true
The result for iteration no. 17 is true
The result for iteration no. 18 is false
The result for iteration no. 19 is true
The result for iteration no. 20 is true
The result for iteration no. 21 is false
The result for iteration no. 22 is true
The result for iteration no. 23 is false
The result for iteration no. 24 is true
The result for iteration no. 25 is true
The result for iteration no. 26 is true
The result for iteration no. 27 is true
The result for iteration no. 28 is false
The result for iteration no. 29 is true
The result for iteration no. 30 is true
The result for iteration no. 31 is true
The result for iteration no. 32 is false
The result for iteration no. 33 is true
The result for iteration no. 34 is true
The result for iteration no. 35 is false
The result for iteration no. 36 is true
The result for iteration no. 37 is true
The result for iteration no. 38 is true
The result for iteration no. 39 is true
The result for iteration no. 40 is true
The result for iteration no. 41 is true
The result for iteration no. 42 is false
The result for iteration no. 43 is true
The result for iteration no. 44 is true
The result for iteration no. 45 is false
The result for iteration no. 46 is true
The result for iteration no. 47 is true
The result for iteration no. 48 is false
The result for iteration no. 49 is true