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
d493e52b
Commit
d493e52b
authored
Oct 05, 2013
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Issue 14927: Minor clean-up of function parameters in random().
parent
9895e08a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
19 additions
and
20 deletions
+19
-20
Lib/random.py
Lib/random.py
+19
-20
No files found.
Lib/random.py
View file @
d493e52b
...
...
@@ -170,29 +170,28 @@ class Random(_random.Random):
## -------------------- integer methods -------------------
def
randrange
(
self
,
start
,
stop
=
None
,
step
=
1
,
int
=
int
,
default
=
None
,
maxwidth
=
1L
<<
BPF
):
def
randrange
(
self
,
start
,
stop
=
None
,
step
=
1
,
_int
=
int
,
_maxwidth
=
1L
<<
BPF
):
"""Choose a random item from range(start, stop[, step]).
This fixes the problem with randint() which includes the
endpoint; in Python this is usually not what you want.
Do not supply the 'int', 'default', and 'maxwidth' arguments.
"""
# This code is a bit messy to make it fast for the
# common case while still doing adequate error checking.
istart
=
int
(
start
)
istart
=
_
int
(
start
)
if
istart
!=
start
:
raise
ValueError
,
"non-integer arg 1 for randrange()"
if
stop
is
default
:
if
stop
is
None
:
if
istart
>
0
:
if
istart
>=
maxwidth
:
if
istart
>=
_
maxwidth
:
return
self
.
_randbelow
(
istart
)
return
int
(
self
.
random
()
*
istart
)
return
_
int
(
self
.
random
()
*
istart
)
raise
ValueError
,
"empty range for randrange()"
# stop argument supplied.
istop
=
int
(
stop
)
istop
=
_
int
(
stop
)
if
istop
!=
stop
:
raise
ValueError
,
"non-integer stop for randrange()"
width
=
istop
-
istart
...
...
@@ -210,14 +209,14 @@ class Random(_random.Random):
# a long, but we're supposed to return an int (for backward
# compatibility).
if
width
>=
maxwidth
:
return
int
(
istart
+
self
.
_randbelow
(
width
))
return
int
(
istart
+
int
(
self
.
random
()
*
width
))
if
width
>=
_
maxwidth
:
return
_
int
(
istart
+
self
.
_randbelow
(
width
))
return
_int
(
istart
+
_
int
(
self
.
random
()
*
width
))
if
step
==
1
:
raise
ValueError
,
"empty range for randrange() (%d,%d, %d)"
%
(
istart
,
istop
,
width
)
# Non-unit step argument supplied.
istep
=
int
(
step
)
istep
=
_
int
(
step
)
if
istep
!=
step
:
raise
ValueError
,
"non-integer step for randrange()"
if
istep
>
0
:
...
...
@@ -230,9 +229,9 @@ class Random(_random.Random):
if
n
<=
0
:
raise
ValueError
,
"empty range for randrange()"
if
n
>=
maxwidth
:
if
n
>=
_
maxwidth
:
return
istart
+
istep
*
self
.
_randbelow
(
n
)
return
istart
+
istep
*
int
(
self
.
random
()
*
n
)
return
istart
+
istep
*
_
int
(
self
.
random
()
*
n
)
def
randint
(
self
,
a
,
b
):
"""Return random integer in range [a, b], including both end points.
...
...
@@ -240,7 +239,7 @@ class Random(_random.Random):
return
self
.
randrange
(
a
,
b
+
1
)
def
_randbelow
(
self
,
n
,
_log
=
_log
,
int
=
int
,
_maxwidth
=
1L
<<
BPF
,
def
_randbelow
(
self
,
n
,
_log
=
_log
,
_
int
=
int
,
_maxwidth
=
1L
<<
BPF
,
_Method
=
_MethodType
,
_BuiltinMethod
=
_BuiltinMethodType
):
"""Return a random int in the range [0,n)
...
...
@@ -257,7 +256,7 @@ class Random(_random.Random):
# has not been overridden or if a new getrandbits() was supplied.
# This assures that the two methods correspond.
if
type
(
self
.
random
)
is
_BuiltinMethod
or
type
(
getrandbits
)
is
_Method
:
k
=
int
(
1.00001
+
_log
(
n
-
1
,
2.0
))
# 2**k > n-1 > 2**(k-2)
k
=
_
int
(
1.00001
+
_log
(
n
-
1
,
2.0
))
# 2**k > n-1 > 2**(k-2)
r
=
getrandbits
(
k
)
while
r
>=
n
:
r
=
getrandbits
(
k
)
...
...
@@ -265,7 +264,7 @@ class Random(_random.Random):
if
n
>=
_maxwidth
:
_warn
(
"Underlying random() generator does not supply
\
n
"
"enough bits to choose from a population range this large"
)
return
int
(
self
.
random
()
*
n
)
return
_
int
(
self
.
random
()
*
n
)
## -------------------- sequence methods -------------------
...
...
@@ -273,20 +272,20 @@ class Random(_random.Random):
"""Choose a random element from a non-empty sequence."""
return
seq
[
int
(
self
.
random
()
*
len
(
seq
))]
# raises IndexError if seq is empty
def
shuffle
(
self
,
x
,
random
=
None
,
int
=
int
):
def
shuffle
(
self
,
x
,
random
=
None
):
"""x, random=random.random -> shuffle list x in place; return None.
Optional arg random is a 0-argument function returning a random
float in [0.0, 1.0); by default, the standard random.random.
Do not supply the 'int' argument.
"""
if
random
is
None
:
random
=
self
.
random
_int
=
int
for
i
in
reversed
(
xrange
(
1
,
len
(
x
))):
# pick an element in x[:i+1] with which to exchange x[i]
j
=
int
(
random
()
*
(
i
+
1
))
j
=
_
int
(
random
()
*
(
i
+
1
))
x
[
i
],
x
[
j
]
=
x
[
j
],
x
[
i
]
def
sample
(
self
,
population
,
k
):
...
...
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