Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
e0afb724
Commit
e0afb724
authored
Jun 30, 2012
by
Mark Dickinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Closes #14591: Random.jumpahead could produce an invalid MT state on 64-bit machines.
parent
9848d812
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
28 additions
and
2 deletions
+28
-2
Lib/test/test_random.py
Lib/test/test_random.py
+8
-0
Misc/NEWS
Misc/NEWS
+3
-0
Modules/_randommodule.c
Modules/_randommodule.c
+17
-2
No files found.
Lib/test/test_random.py
View file @
e0afb724
...
...
@@ -57,6 +57,14 @@ class TestBasicOps(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
self
.
gen
.
jumpahead
)
# needs an arg
self
.
assertRaises
(
TypeError
,
self
.
gen
.
jumpahead
,
2
,
3
)
# too many
def
test_jumpahead_produces_valid_state
(
self
):
# From http://bugs.python.org/issue14591.
self
.
gen
.
seed
(
199210368
)
self
.
gen
.
jumpahead
(
13550674232554645900
)
for
i
in
range
(
500
):
val
=
self
.
gen
.
random
()
self
.
assertLess
(
val
,
1.0
)
def
test_sample
(
self
):
# For the entire allowable range of 0 <= k <= N, validate that
# the sample is of the correct length and contains only unique items
...
...
Misc/NEWS
View file @
e0afb724
...
...
@@ -75,6 +75,9 @@ Core and Builtins
Library
-------
- Issue #14591: Fix bug in Random.jumpahead that could produce an invalid
Mersenne Twister state on 64-bit machines.
- Issue #5346: Preserve permissions of mbox, MMDF and Babyl mailbox
files on flush().
...
...
Modules/_randommodule.c
View file @
e0afb724
...
...
@@ -400,7 +400,7 @@ random_jumpahead(RandomObject *self, PyObject *n)
long
i
,
j
;
PyObject
*
iobj
;
PyObject
*
remobj
;
unsigned
long
*
mt
,
tmp
;
unsigned
long
*
mt
,
tmp
,
nonzero
;
if
(
!
PyInt_Check
(
n
)
&&
!
PyLong_Check
(
n
))
{
PyErr_Format
(
PyExc_TypeError
,
"jumpahead requires an "
...
...
@@ -427,8 +427,23 @@ random_jumpahead(RandomObject *self, PyObject *n)
mt
[
j
]
=
tmp
;
}
for
(
i
=
0
;
i
<
N
;
i
++
)
nonzero
=
0
;
for
(
i
=
1
;
i
<
N
;
i
++
)
{
mt
[
i
]
+=
i
+
1
;
mt
[
i
]
&=
0xffffffffUL
;
/* for WORDSIZE > 32 machines */
nonzero
|=
mt
[
i
];
}
/* Ensure the state is nonzero: in the unlikely event that mt[1] through
mt[N-1] are all zero, set the MSB of mt[0] (see issue #14591). In the
normal case, we fall back to the pre-issue 14591 behaviour for mt[0]. */
if
(
nonzero
)
{
mt
[
0
]
+=
1
;
mt
[
0
]
&=
0xffffffffUL
;
/* for WORDSIZE > 32 machines */
}
else
{
mt
[
0
]
=
0x80000000UL
;
}
self
->
index
=
N
;
Py_INCREF
(
Py_None
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment