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
c324697b
Commit
c324697b
authored
Sep 07, 2010
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Small clean-ups.
parent
e572bcec
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
29 deletions
+12
-29
Lib/random.py
Lib/random.py
+12
-29
No files found.
Lib/random.py
View file @
c324697b
...
@@ -167,7 +167,7 @@ class Random(_random.Random):
...
@@ -167,7 +167,7 @@ class Random(_random.Random):
This fixes the problem with randint() which includes the
This fixes the problem with randint() which includes the
endpoint; in Python this is usually not what you want.
endpoint; in Python this is usually not what you want.
Do not supply the 'int' a
nd 'maxwidth' arguments
.
Do not supply the 'int' a
rgument
.
"""
"""
# This code is a bit messy to make it fast for the
# This code is a bit messy to make it fast for the
...
@@ -186,20 +186,7 @@ class Random(_random.Random):
...
@@ -186,20 +186,7 @@ class Random(_random.Random):
raise
ValueError
(
"non-integer stop for randrange()"
)
raise
ValueError
(
"non-integer stop for randrange()"
)
width
=
istop
-
istart
width
=
istop
-
istart
if
step
==
1
and
width
>
0
:
if
step
==
1
and
width
>
0
:
# Note that
return
istart
+
self
.
_randbelow
(
width
)
# int(istart + self.random()*width)
# instead would be incorrect. For example, consider istart
# = -2 and istop = 0. Then the guts would be in
# -2.0 to 0.0 exclusive on both ends (ignoring that random()
# might return 0.0), and because int() truncates toward 0, the
# final result would be -1 or 0 (instead of -2 or -1).
# istart + int(self.random()*width)
# would also be incorrect, for a subtler reason: the RHS
# can return a long, and then randrange() would also return
# a long, but we're supposed to return an int (for backward
# compatibility).
return
int
(
istart
+
self
.
_randbelow
(
width
))
if
step
==
1
:
if
step
==
1
:
raise
ValueError
(
"empty range for randrange() (%d,%d, %d)"
%
(
istart
,
istop
,
width
))
raise
ValueError
(
"empty range for randrange() (%d,%d, %d)"
%
(
istart
,
istop
,
width
))
...
@@ -233,20 +220,16 @@ class Random(_random.Random):
...
@@ -233,20 +220,16 @@ class Random(_random.Random):
by a single call to the underlying generator.
by a single call to the underlying generator.
"""
"""
try
:
getrandbits
=
self
.
getrandbits
getrandbits
=
self
.
getrandbits
# Only call self.getrandbits if the original random() builtin method
except
AttributeError
:
# has not been overridden or if a new getrandbits() was supplied.
pass
# This assures that the two methods correspond.
else
:
if
type
(
self
.
random
)
is
_BuiltinMethod
or
type
(
getrandbits
)
is
_Method
:
# Only call self.getrandbits if the original random() builtin method
k
=
n
.
bit_length
()
# don't use (n-1) here because n can be 1
# has not been overridden or if a new getrandbits() was supplied.
r
=
getrandbits
(
k
)
# 0 <= r < 2**k
# This assures that the two methods correspond.
while
r
>=
n
:
if
type
(
self
.
random
)
is
_BuiltinMethod
or
type
(
getrandbits
)
is
_Method
:
r
=
getrandbits
(
k
)
k
=
n
.
bit_length
()
# don't use (n-1) here because n can be 1
return
r
r
=
getrandbits
(
k
)
# 0 <= r < 2**k
while
r
>=
n
:
r
=
getrandbits
(
k
)
return
r
if
n
>=
_maxwidth
:
if
n
>=
_maxwidth
:
_warn
(
"Underlying random() generator does not supply
\
n
"
_warn
(
"Underlying random() generator does not supply
\
n
"
"enough bits to choose from a population range this large"
)
"enough bits to choose from a population range this large"
)
...
...
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