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

标签 algorithm random

我正在尝试为我的模拟器重现一些行为。

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

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

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

  • 如果检查失败一次则增加下一次检查百分比
  • 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;
    }
}

那么如果你这样调用它:

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

关于算法根据之前的结果调整 RNG,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34722874/

相关文章:

string - 百分比 diff b/t 两个不同长度的字符串

c# - 如何评估 C# 中的自定义括号表达式?

c - 在 C 中查找整数中最高设置位(msb)的最快/最有效的方法是什么?

javascript - 如何执行符合我需要的 Javascript 对象递归搜索?

c# - 在 C# 中生成随机盐的最佳方式?

algorithm - 具有最小冲突的多代理策略分配

java - 随机方向

sql - 为什么我在 postgres 中使用随机函数在 select 中获得多条记录?

python - 如何在python中生成具有两个精度的浮点随机数

javascript - 以随机顺序打印数组?