Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 基于先前结果的自适应RNG算法_Algorithm_Random - Fatal编程技术网

Algorithm 基于先前结果的自适应RNG算法

Algorithm 基于先前结果的自适应RNG算法,algorithm,random,Algorithm,Random,我正试图为我的模拟器重现一些行为 我有一张百分比支票,从30%到70%不等。问题是它不是严格随机的。以下是我提取的实时数据: 我从数据中注意到一些事情: 如果检查失败一次,则增加下一次检查的百分比 70%检查不会连续两次失败 所有的第一轮似乎都有20%的低成功率 生成数据的服务器使用PHP,因此它可能使用已知为伪随机的rand()函数 所以我想在Java中重现这种行为。我可以按照这些模式编写我自己的随机类,但我不确定我的实时数据是否涵盖所有情况,所以我想知道:现有的PRNG算法是否符合这种

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

我有一张百分比支票,从30%到70%不等。问题是它不是严格随机的。以下是我提取的实时数据:

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

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

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

这是如此简单和具体,以至于不太可能,有人已经发布了这方面的信息。我很荣幸能成为第一个

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;
    }
}

给我们看看你到目前为止得到的代码。听起来你只需要几个标志。一个用于确定这是第一次检查,另一个用于指示上一次检查是通过还是失败。然后相应地设置百分比(50/70/100)。很明显,无论你检查什么,都不是一系列独立的随机变量,这是随机数生成器设计用来产生的。连续成功的次数有限制吗?第一次不同的是什么?考虑一个检查某个整数变量的最后十进制数字的检查。如果最后一个十进制数字为0、1或2,则检查失败。您可以在0到5(包括0到5)的范围内统一生成整数变量的第一个值。每次检查后,向变量中添加7,结果将用于下一次检查。这具有您要求的属性,但显然不是随机的。你正在建模的现象实际上是随机的吗?如果是,你怎么知道?也许你试图建模的是一个马尔可夫过程。
FailGenerator
如上所述模拟了一个马尔可夫过程。在第一个状态之后,转换为false->true(100%)、true->true(70%)、false(30%)。从长远来看,因为这个过程只有在30%的“真”结果之后才会给你“假”,而在“假”结果之后就不会给你“假”,所以它在10/13的时间里是“真”的,或者说在76.9%的时间里是“真”的,而不是70%。也许这就是OP想要的;不清楚,这和我要找的不符。我需要的是一种能够将自己调整到全球70%的命中率的东西,即使每次命中后都有100%的几率被错过。@crystak-确实如此,你唯一需要改变的是David K写给你的东西->将概率更改为50%,然后>
从长远来看,这个模型平均产生66.67%的“未命中”
。使这项工作有效的唯一方法是更改这一行
this(0.5,0.7)此<代码>此(0.5,0.5)@libik谢谢。但是,在您的示例中,它不应该是
r.nextDouble()
,因为nextDouble可以返回0,并且0%的概率应该总是失败的吗?
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