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
1d8d8da8
Commit
1d8d8da8
authored
Jun 19, 2003
by
Tim Peters
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
randrange(): Repaired my overly optimistic rewrite, and added comments
explaining what's wrong with the two simpler variants.
parent
e3bcbc10
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
1 deletion
+13
-1
Lib/random.py
Lib/random.py
+13
-1
No files found.
Lib/random.py
View file @
1d8d8da8
...
...
@@ -148,7 +148,19 @@ class Random(_random.Random):
if
istop
!=
stop
:
raise
ValueError
,
"non-integer stop for randrange()"
if
step
==
1
and
istart
<
istop
:
return
int
(
istart
+
self
.
random
()
*
(
istop
-
istart
))
# Note that
# int(istart + self.random()*(istop - istart))
# 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()*(istop - istart))
# 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
+
int
(
self
.
random
()
*
(
istop
-
istart
)))
if
step
==
1
:
raise
ValueError
,
"empty range for randrange()"
...
...
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