26
1
The gambler's fallacy is a cognitive bias where we mistakenly expect things that have occurred often to be less likely to occur in the future and things that have not occurred in a while to be more likely to happen soon. Your task is to implement a specific version of this.
Challenge Explanation
Write a function that returns a random integer between 1 and 6, inclusive. The catch: the first time the function is run, the outcome should be uniform (within 1%), however, each subsequent call will be skewed in favor of values that have been rolled fewer times previously. The specific details are as follows:
- The die remembers counts of numbers generated so far.
- Each outcome is weighted with the following formula: \$count_{max} - count_{die} + 1\$
- For instance, if the roll counts so far are \$[1, 0, 3, 2, 1, 0]\$, the weights will be \$[3, 4, 1, 2, 3, 4]\$, that is to say that you will be 4 times more likely to roll a \$2\$ than a \$3\$.
- Note that the formula means that a roll outcome of \$[a, b, c, d, e, f]\$ is weighted the same as \$[a + n, b + n, c + n, d + n, e + n, f + n]\$
Rules and Assumptions
- Standard I/O rules and banned loopholes apply
- Die rolls should not be deterministic. (i.e. use a PRNG seeded from a volatile source, as is typically available as a builtin.)
- Your random source must have a period of at least 65535 or be true randomness.
- Distributions must be within 1% for weights up to 255
- 16-bit RNGs are good enough to meet both the above requirements. Most built-in RNGs are sufficient.
- You may pass in the current distribution as long as that distribution is either mutated by the call or the post-roll distribution is returned alongside the die roll. Updating the distribution/counts is a part of this challenge.
- You may use weights instead of counts. When doing so, whenever a weight drops to 0, all weights should increase by 1 to achieve the same effect as storing counts.
- You may use these weights as repetitions of elements in an array.
Good luck. May the bytes be ever in your favor.
It appears you can comply with all the rules and banned loopholes by starting with a random number n, then outputting (n++ % 6). – Fax – 2019-05-18T10:17:54.210
2@Fax This problem specifies explicitly and exactly what the distribution of the $k$th number should be given the first $k-1$ numbers.Your idea gives obviously the wrong distribution for the second number given the first number. – JiK – 2019-05-18T12:30:18.983
@JiK I disagree, as that argument could be used against any other code that uses a PRNG as opposed to true random. My proposal is a PRNG, albeit a very simplistic one. – Fax – 2019-05-18T13:23:40.010
@JiK Assuming you're talking about theoretical distribution, that is. Measured distribution is within the required 1% for a $k$ large enough to have statistical significance. – Fax – 2019-05-18T13:58:11.553
1@Fax Your random source doesn't have a period of at least 65535, so it's not a PRNG sufficient for this problem. Also I don't understand what you mean by "measured distribution". – JiK – 2019-05-18T17:18:30.377
@JiK That is false, the period of n++ is limited only by its implementation. As for "measured distribution", I meant frequency distribution as opposed to "theoretical distribution", i.e. probability distribution. – Fax – 2019-05-20T14:01:29.347
What do you mean by frequency distribution in this case? How do you show that your frequency distribution corresponds to the distribution that is wanted in this problem? – JiK – 2019-05-20T14:33:27.607
@JiK It doesn't. I was thinking how serial rolls converge towards uniform probability, and it took me a while to realize that you have to perform parallel rolls and group by previous output to get the measurement desired in rule #3. My original proposal doesn't meet that requirement (nor does any solution that uses a fixed seed). – Fax – 2019-05-20T15:22:59.767
@Fax technically that would not be a correct distribution if you simply cycle through die rolls; the distribution would be 100/0/0/0/0/0 for the first roll. I just explicitly banned that technique anyway. – Beefster – 2019-05-20T16:21:14.060