简体   繁体   English

Java的Random类行为说明

[英]Java's Random class behaviour clarification

I have a class 'A' that holds an instance of Random, and which simulates rolling a die, eg 6 sides, with a result of 1 - 6 being generated on each 'roll'. 我有一个类'A',它拥有一个Random实例,它模拟掷骰子(例如6面)的滚动,每个'roll'生成1-6的结果。

I have another class 'B' that may hold a reference to an object of type A, and should delegate to its 'roll' method. 我还有另一个类“ B”,该类可能包含对A类型对象的引用,并且应该委托给它的“ roll”方法。

Another person needs to write a unit test to assess that the method in class B is delegating to the 'roll' method in class A correctly. 另一个人需要编写一个单元测试来评估B类中的方法是否正确地委托给A类中的“ roll”方法。 In order to do this I simply need to invoke the method X times and check that the score updates. 为此,我只需要调用方法X次并检查分数是否更新。

Currently in the unit test I have a loop that goes round up to 1000 times and if the score hasn't been updated then I assume it is beyond the realms of possibility that the same number could have been rolled consecutively 1000 times! 目前在单元测试中,我有一个循环,可以循环到1000次,如果尚未更新分数,那么我认为相同的数字可以连续滚动1000次超出了可能性范围! I have however picked the number 1000 out of thin air. 但是,我凭空选择了1000。 I wanted to pick a more accurate number, based on the actual behaviour of Java's Random class. 我想根据Java的Random类的实际行为选择一个更准确的数字。

From reading the API it says it is states it is "a linear congruential pseudorandom number generator..". 通过阅读该API可以说它是“线性同余伪随机数生成器”。 What I'm trying to ascertain is based on it using a 48-bit, is there a number of times for which it is impossible for the same value to be repeated. 我要确定的是基于48位的,是否有很多次不可能重复相同的值。 Eg in my scenario where the numbers 1 - 6 could be generated, is it say impossible to get the same number, eg 6, more than 20 times in a row? 例如,在我的情况下,可以生成数字1-6,难道说不可能连续获得20次以上的相同数字(例如6)吗? I'm looking for that information if anyone knows it. 如果有人知道,我正在寻找该信息。

UPDATE -- I'll make the question simpler. 更新-我将使问题更简单。 With the Java random class, if I call nextInt(6), how many times is it possible for me to receive the same result consecutively. 对于Java随机类,如果我调用nextInt(6),那么我有可能连续接收到相同的结果多少次。 Eg mathematically is there a limit based on the 48-bit seed and workings of the algorithm of the Random class? 例如,数学上是否存在基于48位种子和Random类算法的工作原理的限制? I want an answer that states, eg "It is categorically impossible to get the same result more than X times" where X is my answer. 我想要一个答案,例如“绝对不可能获得超过X倍的相同结果”,其中X是我的答案。

Typically you would try to mock class A (ie with Mockito). 通常,您会尝试模拟A类(即使用Mockito)。 Here is an SO question/ answer that shows how to verify if a method was called n times. 是一个SO问题/解答,显示了如何验证某个方法是否被调用过n次。

The PRNG used by Random has only 2 48 different states according to the documentation. 根据文档, Random使用的PRNG仅具有2 48个不同的状态。 The sequence of numbers generated with nextInt(6) therefore repeats with a period of at most 2 48 . 因此,用nextInt(6)生成的数字序列最多重复2 48个周期。

We can assume that all 6 possible outcomes are generated eventually independent of the seed value, otherwise, the generator is of very bad quality. 我们可以假设所有6种可能的结果最终都与种子值无关地生成,否则生成器的质量很差。 Therefore, there must be a limit X < 2 48 satisfying "It is categorically impossible to get the same result more than X times". 因此,必须满足X <2 48的限制,“绝对不可能获得超过X次的相同结果”。

To find the smallest such X is however not trivial. 然而,找到最小的X并非易事。 The brute force method would generate 2 48 sucessive numbers and check. 蛮力方法将生成2 48个成功数字并进行检查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM