diff --git a/Lib/random.py b/Lib/random.py index 875beb2f8cf41c..768b100786343c 100644 --- a/Lib/random.py +++ b/Lib/random.py @@ -246,7 +246,7 @@ def _randbelow_with_getrandbits(self, n): "Return a random int in the range [0,n). Defined for n > 0." getrandbits = self.getrandbits - k = n.bit_length() + k = (n - 1).bit_length() r = getrandbits(k) # 0 <= r < 2**k while r >= n: r = getrandbits(k) diff --git a/Lib/test/test_generators.py b/Lib/test/test_generators.py index d48f0d47ba1962..1a4d2d4393669e 100644 --- a/Lib/test/test_generators.py +++ b/Lib/test/test_generators.py @@ -1164,22 +1164,22 @@ def b(): A->F B->B C->C D->D E->F F->F G->G H->H I->I J->J K->B L->L M->M merged D into C A->F B->B C->C D->C E->F F->F G->G H->H I->I J->J K->B L->L M->M -merged M into C - A->F B->B C->C D->C E->F F->F G->G H->H I->I J->J K->B L->L M->C -merged J into B - A->F B->B C->C D->C E->F F->F G->G H->H I->I J->B K->B L->L M->C +merged M into B + A->F B->B C->C D->C E->F F->F G->G H->H I->I J->J K->B L->L M->B +merged H into G + A->F B->B C->C D->C E->F F->F G->G H->G I->I J->J K->B L->L M->B merged B into C - A->F B->C C->C D->C E->F F->F G->G H->H I->I J->C K->C L->L M->C -merged F into G - A->G B->C C->C D->C E->G F->G G->G H->H I->I J->C K->C L->L M->C -merged L into C - A->G B->C C->C D->C E->G F->G G->G H->H I->I J->C K->C L->C M->C -merged G into I - A->I B->C C->C D->C E->I F->I G->I H->H I->I J->C K->C L->C M->C -merged I into H - A->H B->C C->C D->C E->H F->H G->H H->H I->H J->C K->C L->C M->C -merged C into H - A->H B->H C->H D->H E->H F->H G->H H->H I->H J->H K->H L->H M->H + A->F B->C C->C D->C E->F F->F G->G H->G I->I J->J K->C L->L M->C +merged C into G + A->F B->G C->G D->G E->F F->F G->G H->G I->I J->J K->G L->L M->G +merged G into J + A->F B->J C->J D->J E->F F->F G->J H->J I->I J->J K->J L->L M->J +merged J into F + A->F B->F C->F D->F E->F F->F G->F H->F I->I J->F K->F L->L M->F +merged L into F + A->F B->F C->F D->F E->F F->F G->F H->F I->I J->F K->F L->F M->F +merged I into F + A->F B->F C->F D->F E->F F->F G->F H->F I->F J->F K->F L->F M->F """ # Emacs turd ' diff --git a/Lib/test/test_random.py b/Lib/test/test_random.py index b1e4ef4197d130..1ad702102c0115 100644 --- a/Lib/test/test_random.py +++ b/Lib/test/test_random.py @@ -800,7 +800,7 @@ def test_randrange_uses_getrandbits(self): self.gen.seed(1234567) # If randrange uses getrandbits, it should pick getrandbits(100) # when called with a 100-bits stop argument. - self.assertEqual(self.gen.randrange(2**99), + self.assertEqual(self.gen.randrange(2**99 + 1), 97904845777343510404718956115) def test_randbelow_logic(self, _log=log, int=int):