- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试为我的模拟器重现一些行为。
我有一个百分比支票,从 30% 到 70% 不等。问题是它不是严格随机的。这是我提取的实时数据:
https://docs.google.com/spreadsheets/d/1JLXRxh7xvR_fYwnTtvQ72jNV0v4lHiEEY6o-kG52wQA/pubhtml
我从数据中注意到的一些事情:
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/
我开发了一个 RNG 算法并生成随机数,现在我想测试生成的输出的随机性。我从 nist(sts) 下载了一个测试套件。有一个选项可以提供输入二进制文件来测试随机性。但我不知道如何生成这些二进制文件。我
因此,我编写了一个函数(以及一个 RNG 函数,上述函数调用该函数)来将随机数量的星号打印到控制台窗口,直到达到 90 个空格。星号代表汽车的运动,90个空格是轨道的长度。下面我包含的代码打印随机数量
关闭。这个问题是opinion-based .它目前不接受答案。 想要改进这个问题? 更新问题,以便 editing this post 可以用事实和引用来回答它. 关闭 8 年前。 Improve
我正在尝试为我的模拟器重现一些行为。 我有一个百分比支票,从 30% 到 70% 不等。问题是它不是严格随机的。这是我提取的实时数据: https://docs.google.com/spreadsh
这个问题在这里已经有了答案: How to generate a random integer in the range [0,n] from a stream of random bits wit
我目前正在实现一个代表 52 张牌的牌组的 Deck 类。它使用 boost Random 库来洗牌代表卡片的整数。 #include #include #include "constants.h
在本文的最后一个示例 (http://www.daniweb.com/software-development/cpp/threads/1769/c-random-numbers) 中,作者声称这是一
我想使用 在我的 C++ 应用程序中使用多个 RNG .播种它们的最佳方法是什么?我担心当使用来自单个 RNG 的随机数为每个 RNG 播种时,来自不同 RNG 的随机数会过于相关。 最佳答案 如果
现在我正在使用 Mersenne Twister RNG 并执行 Fisher-Yates 洗牌算法 100 次: std::vector shufCards; for(int i =
我正在开发一款纸牌游戏,我需要洗牌算法来做得很好,并且每次游戏运行时都不同,并且没有可预测的纸牌序列。 我正在使用 Mersenne twister 算法,但它仍然需要一个种子,所以实际上,虽然它产生
我可以使用两种方法中的一种来创建具有两个重要特征的伪随机数序列 - (1) 它可以在不同的机器上重现,并且 (2) 该序列从不重复范围内的数字,直到所有数字都被发出. 我的问题是 - 这些方法中的任何
我有一段冗长而复杂的源代码,它使用带有修复种子的随机数生成器。 这段代码是一个模拟器,这个模拟器的参数就是这个RNG给出的随机值。当我在同一台机器上执行代码时,无论我尝试多少次,输出都是一样的。但是当
我在想这个问题。我听说全局变量不好,它们会损害代码的可维护性、可用性、可重用性等。但在这种情况下,我还能做什么呢?也就是说,我有一个“伪随机数生成器”(PRNG),正如人们所知,它们涉及一种内部状态,
回复 this question我运行了以下 VBA 实验: Sub Test() Dim i As Long, A As Variant Dim count1 As Long, co
我知道我可以用例如设置 RNGversion RNGversion("3.5.2") 但是是否可以查询我当前使用的 R 版本? 编辑: 我的问题是版本 3.6.0 发生了一些变化,这在 RNGkind
我今天刚开始学习JavaScript,这个很简单的问题给我带来了麻烦。这只是较大代码段的一部分,但我将其隔离以尝试修复它。这是: document.write(blarg()); func
我制作了一个随机数生成器,如果有人在没有数字的情况下使用它,它会崩溃如何确保用户输入是否是字符串,它不会崩溃,而是说“无效参数”? static int random; static String s
好吧,我正在编写一个与 cpu 对抗的程序,但每次它都会先失败然后致命,我会分解我的代码,以便让你们更容易提供帮助。 进口: import javax.swing.JOptionPane; impor
我的问题是http://rcpp-devel.r-forge.r-project.narkive.com/qJMEsvOK/setting-the-r-random-seed-from-rcpp的后续
作为一个学习 Haskell 的 Java 人,我已经习惯了思考一切的新方法,但我花了半天时间尝试用简单的 RNG 实现一些东西,但一无所获。在 Java 中,我可以创建一个静态 RNG 并使用 Cl
我是一名优秀的程序员,十分优秀!