- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个简短的程序来为我玩骰子游戏(普通的 6 面骰子)。第一卷的数字被添加到分数中。第一次掷骰后,如果我掷出 6,则游戏停止并记录分数(不加上 6)。如果第一次掷出 6,那很好,它会像任何其他数字 1 到 5 一样被添加。我正在尝试运行这个游戏的一系列迭代,这样我就有了一个很长的分数列表(a半身像是滚动的 6)。我将这些分数重新排列为从小到大的顺序,然后找到列表的中位数,即最适合停止的分数。
出于某种原因,当我运行程序时,我总是得到 13,但我知道答案应该是 15。在 Java 中使用 Random 会对中位数产生某种影响吗?我不完全知道 Random 如何生成数字以及它是否以平等的机会创造它们。另外,有没有什么东西突然出现但不应该起作用?
import java.util.*;
public class DiceRoller {
private static Random r = new Random();
private static final int games = 10001;
private static int[] totalScores = new int[games];
private static int index = 0;
public static void main(String[] args) {
int score = 0; boolean firstRoll = true;
while (index < games) {
int roll = roll();
if (firstRoll) {
score += roll;
firstRoll = false;
} else {
if (roll == 6) {
totalScores[index] = score;
index++;
score = 0; firstRoll = true;
} else {
score += roll;
}
}
}
System.out.println("The median is " + median() + ".");
}
public static int roll() {
return r.nextInt(6) + 1;
}
public static int median() {
Arrays.sort(totalScores);
int temp = totalScores[games / 2];
return temp;
}
}
最佳答案
您得到 13
因为这是正确的结果。一点数学知识:如果 S
是代表这些游戏中任何一场得分的随机变量,那么您可以考虑 Probability generating function S
的 f(z)
。从游戏的描述来看,这个概率生成函数满足方程:
f(z) = (z + z^2 + z^3 + z^4 + z^5 + z^6)/36 + f(z)(z + z^2 + z^ 3 + z^4 + z^5)/6
这需要一些思考,或者熟悉这种结构:右侧的左侧项考虑了在简单的 2 掷游戏中获得 1 到 6 的概率;涉及 f(z) 的右侧项考虑了涉及 3 次或更多次掷骰的游戏,用最后的 pre-6 掷骰(必须在 1 到 5 范围内)和前面的掷骰来表示它们,其概率我们可以再次使用f
递归表达。
无论如何,走到这一步之后,我们可以重新安排将f
描述为z
的有理函数,然后展开为幂级数,开始:
f(z) = 1/36*z + 7/216*z^2 + 49/1296*z^3 + 343/7776*z^4 + 2401/46656*z^5 + 16807/279936*z^6 + 63217/1679616*z^7 + 388087/10077696*z^8 + 2335585/60466176*z^9 + 13681927/362797056*z^10 + 77103313/2176782336*z^ 11 + 409031959/13060694016 *z^12 + 2371648321/78364164096*z^13 + 13583773735/470184984576*z^14 + ...
(我用 Pari/GP 得到这个。)
z^k
的系数则描述了游戏值为k
的概率;因此,得分为 1 的概率为 36 分之一,得分为 2 的概率为 216 分之 7,依此类推。前 12 个系数之和为 0.472828864487196328...
,而前 13 个系数之和为 0.5030933144224321950968...
。所以中位数确实是 13。
为了提供独立检查,我编写了一个快速的 Python 程序:
from __future__ import division
import random
def roll():
return random.randint(1, 6)
def play():
score = roll()
while True:
throw = roll()
if throw == 6:
break
score += throw
return score
all_scores = sorted(play() for _ in xrange(1000001))
print "median is: ",all_scores[len(all_scores) // 2]
print "fraction of scores <= 12: ",all_scores.index(13) / len(all_scores)
print "fraction of scores <= 13: ",all_scores.index(14) / len(all_scores)
果然,结果如下:
iwasawa:~ mdickinson$ python dice_game.py
median is: 13
fraction of scores <= 12: 0.472811527188
fraction of scores <= 13: 0.502863497137
因此,为了回答您的问题,您看到的结果并不是 Java 随机数生成存在任何弱点的证据。
关于java - Java的Random Object是不是通过均等的机会来产生随机数的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7297660/
在分析代码时,我偶然发现了以下代码段: msk = np.random.rand(len(df)) < 0.8 变量“msk”和“df”与我的问题无关。经过一些研究,我认为这种用法也与“随机”类有关。
出于几个合理的原因,我必须使用 BSD 的 random() 来生成非常大量的随机数,并且由于它的周期很短(~2^69,如果我没记错的话),这些数字的质量会降低对于我的用例来说很快。我可以使用我可以访
每种语言都有一个 random() 函数或类似的东西来生成伪随机数。我想知道下面会发生什么来生成这些数字?我没有编写任何需要这些知识的东西,只是想满足我自己的好奇心。 最佳答案 唐纳德·克努斯开创性的
我开发了一个简单的应用程序来生成测试数据系列,并且我使用随机种子将其构建为能够重复。我注意到以下情况并想知道为什么会这样: >>> random.seed(1) >>> [random.randint
random() * random() 和 random() ** 2 有区别吗? random() 从均匀分布中返回一个介于 0 和 1 之间的值。 在测试两个版本的随机平方数时,我注意到了一点不同
我发现 Python(及其生态系统)充满了奇怪的约定和不一致,这是另一个例子: np.random.rand Create an array of the given shape and popula
我想看看哪个随机数生成器包在我的神经网络中速度更快。 我目前正在从github上修改一段代码,其中numpy.random和random包都用于生成随机整数、随机选择、随机样本等。 我更改此代码的原因
我有一个 Python 大脚本。我在其他人的代码中启发了自己,所以我最终使用 numpy.random 模块来做一些事情(例如创建一个从二项分布中获取的随机数数组),在其他地方我使用模块 random
仅仅是因为“大型 API 综合症”还是生成在某些情况下更偏向的随机数?如果是……我认为控制偏见很重要。 最佳答案 他们是一样的,真的。只是一个方便的方法。检查 javadoc here .此外,您
我只是观察到,当使用 Python3 时,使用 random.shuffle 对列表进行洗牌需要大约一半的运行时间,而当为 显式提交函数 random.random >random 关键字参数。我检查
在python中随机模块,random.uniform()和random.random()有什么区别?它们都生成伪随机数,random.uniform() 生成均匀分布的数字,random.rando
是否可以在JMeter中生成“随机数”变量? 我已经记录了用户旅程 我已将旅程导入JMeter 我必须在用户旅程测试用例中输入一个唯一的4位数ID 在jmeter的当前默认值为2323 有没有一种方法
例如,如果我执行命令两次:ffmpeg -i input.mp4 -vf geq=r='random(1)*255':g='random(1)*255':b='random(1)*255' -stri
尽管随机生成器只创建一次,但输出始终是相同的随机结果(对于所有三个测试输出)。 来自稍大脚本的测试片段: let myRandGen = System.Random() let getRa
我正计划使用IntRange.random()(即(0..9999).random())在 Kotlin 中生成一个随机的5位代码。重要的是,恶意人员不能预测将要生成的数字的顺序。 IntRange.
您能否告诉我如何将 KDB 中的随机数生成器种子设置为或多或少的“随机”数? 我正在尝试执行以下操作: \S .z.i 但不知何故它不起作用。\S 似乎需要一个显式整数,而不是一个变量。 非常感谢!
我需要同时获得 /dev/random和 /dev/urandom在内核模块中。 get_random_bytes提供获取 /dev/urandom 的 API . 但是/dev/random 没有A
random.shuffle(lst_shuffle, random.random) 我知道后一部分是可选参数。但它究竟做了什么。我不明白这是什么意思。 这是来自文档。 random.random()
在树莓派 3 上: >>> import random >>> random.seed(0.9849899567458751) >>> random.random() 0.47871160253065
说我有一些python代码: import random r=random.random() r的值通常从哪里来? 如果我的操作系统没有随机数,那么它将在何处播种呢? 为什么不建议将其用于加密?有什么
我是一名优秀的程序员,十分优秀!