From 7f1bc4c55457b95f82e734bdfee81fed32ee7901 Mon Sep 17 00:00:00 2001
From: ulif <uli@gnufix.de>
Date: Fri, 3 Mar 2017 11:57:17 +0100
Subject: [PATCH] Expect roll nums following the old procedure.

In order to keep all entropy we could in case of sequence lengths
unequal to powers of the number of dice sides just discard rolls and
require further ones, until we get a number in the allowed range. But
this could potentially require an endless number of rolls.

Instead we implement the following policy:
If R=log(<sequence_len>) with <number_of_dice_sides> as basis is >= 1,
we require int(R) rolls. We will ignore elements in the sequence with
index > int(R).

Sample: For 6 dice sides and a sequence of 215 (= 6**3 - 1) elements R
would be log_6(215) = 2.9975... but we would pick an element from the
range [0..35], i.e. (6**2 - 1).

If R is 0, we will require exactly one roll, if <sequence_length> is a
divisor of <dice_sides>.

In all other cases, <sequence_length> is not a divisor of <dice_sides>
and <sequence_length> is shorter than the number of dice_sides. In this
case we will roll dice until we get a result in [1..<sequence_length>].
Other rolls will simply be discarded.
---
 tests/test_random_sources.py | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tests/test_random_sources.py b/tests/test_random_sources.py
index ed3d11c..0b42447 100644
--- a/tests/test_random_sources.py
+++ b/tests/test_random_sources.py
@@ -315,8 +315,8 @@ class TestRealDiceRandomSource(object):
         src = RealDiceRandomSource(argparse.Namespace(dice_sides=2))
         assert src.get_num_rolls(2) == 1
         assert src.get_num_rolls(2**12) == 12
-        assert src.get_num_rolls(3) == 2
-        assert src.get_num_rolls(2**12 + 1) == 13
+        assert src.get_num_rolls(3) == 1
+        assert src.get_num_rolls(2**12 + 1) == 12
 
     def test_main_with_realdice_source(
             self, argv_handler, capsys, fake_input):
-- 
GitLab