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
05a505f1
Commit
05a505f1
authored
Sep 07, 2010
by
Raymond Hettinger
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Minor refactoring and cleanup. Extend looping randrange() technique to subclasses.
parent
e339651b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
23 additions
and
20 deletions
+23
-20
Lib/random.py
Lib/random.py
+23
-20
No files found.
Lib/random.py
View file @
05a505f1
...
@@ -212,25 +212,33 @@ class Random(_random.Random):
...
@@ -212,25 +212,33 @@ class Random(_random.Random):
return
self
.
randrange
(
a
,
b
+
1
)
return
self
.
randrange
(
a
,
b
+
1
)
def
_randbelow
(
self
,
n
,
int
=
int
,
_maxwidth
=
1
<<
BPF
,
type
=
type
,
def
_randbelow
(
self
,
n
,
int
=
int
,
bpf
=
BPF
,
type
=
type
,
_Method
=
_MethodType
,
_
BuiltinMethod
=
_BuiltinMethodType
):
Method
=
_MethodType
,
BuiltinMethod
=
_BuiltinMethodType
):
"""Return a random int in the range [0,n). Raises ValueError if n==0.
"""Return a random int in the range [0,n). Raises ValueError if n==0.
"""
"""
k
=
n
.
bit_length
()
# don't use (n-1) here because n can be 1
getrandbits
=
self
.
getrandbits
getrandbits
=
self
.
getrandbits
# Only call self.getrandbits if the original random() builtin method
# Only call self.getrandbits if the original random() builtin method
# has not been overridden or if a new getrandbits() was supplied.
# has not been overridden or if a new getrandbits() was supplied.
# This assures that the two methods correspond.
# This assures that the two methods correspond.
if
type
(
self
.
random
)
is
_BuiltinMethod
or
type
(
getrandbits
)
is
_Method
:
if
type
(
self
.
random
)
is
BuiltinMethod
or
type
(
getrandbits
)
is
Method
:
k
=
n
.
bit_length
()
# don't use (n-1) here because n can be 1
r
=
getrandbits
(
k
)
# 0 <= r < 2**k
r
=
getrandbits
(
k
)
# 0 <= r < 2**k
while
r
>=
n
:
while
r
>=
n
:
r
=
getrandbits
(
k
)
r
=
getrandbits
(
k
)
return
r
return
r
if
n
>=
_maxwidth
:
# There's an overriden random() method but no new getrandbits() method,
# so we can only use random() from here.
if
k
>
bpf
:
_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"
)
return
int
(
self
.
random
()
*
n
)
return
int
(
self
.
random
()
*
n
)
random
=
self
.
random
N
=
1
<<
k
r
=
int
(
N
*
random
())
while
r
>=
n
:
r
=
int
(
N
*
random
())
return
r
## -------------------- sequence methods -------------------
## -------------------- sequence methods -------------------
...
@@ -249,16 +257,11 @@ class Random(_random.Random):
...
@@ -249,16 +257,11 @@ class Random(_random.Random):
float in [0.0, 1.0); by default, the standard random.random.
float in [0.0, 1.0); by default, the standard random.random.
"""
"""
if
random
is
None
:
randbelow
=
self
.
_randbelow
for
i
in
reversed
(
range
(
1
,
len
(
x
))):
for
i
in
reversed
(
range
(
1
,
len
(
x
))):
# pick an element in x[:i+1] with which to exchange x[i]
# pick an element in x[:i+1] with which to exchange x[i]
j
=
self
.
_randbelow
(
i
+
1
)
j
=
randbelow
(
i
+
1
)
if
random
is
None
else
int
(
random
()
*
(
i
+
1
))
x
[
i
],
x
[
j
]
=
x
[
j
],
x
[
i
]
x
[
i
],
x
[
j
]
=
x
[
j
],
x
[
i
]
else
:
for
i
in
reversed
(
range
(
1
,
len
(
x
))):
# pick an element in x[:i+1] with which to exchange x[i]
j
=
int
(
random
()
*
(
i
+
1
))
x
[
i
],
x
[
j
]
=
x
[
j
],
x
[
i
]
def
sample
(
self
,
population
,
k
):
def
sample
(
self
,
population
,
k
):
"""Chooses k unique random elements from a population sequence or set.
"""Chooses k unique random elements from a population sequence or set.
...
@@ -292,7 +295,7 @@ class Random(_random.Random):
...
@@ -292,7 +295,7 @@ class Random(_random.Random):
population
=
tuple
(
population
)
population
=
tuple
(
population
)
if
not
isinstance
(
population
,
_collections
.
Sequence
):
if
not
isinstance
(
population
,
_collections
.
Sequence
):
raise
TypeError
(
"Population must be a sequence or Set. For dicts, use list(d)."
)
raise
TypeError
(
"Population must be a sequence or Set. For dicts, use list(d)."
)
rand
om
=
self
.
random
rand
below
=
self
.
_randbelow
n
=
len
(
population
)
n
=
len
(
population
)
if
not
0
<=
k
<=
n
:
if
not
0
<=
k
<=
n
:
raise
ValueError
(
"Sample larger than population"
)
raise
ValueError
(
"Sample larger than population"
)
...
@@ -304,16 +307,16 @@ class Random(_random.Random):
...
@@ -304,16 +307,16 @@ class Random(_random.Random):
# An n-length list is smaller than a k-length set
# An n-length list is smaller than a k-length set
pool
=
list
(
population
)
pool
=
list
(
population
)
for
i
in
range
(
k
):
# invariant: non-selected at [0,n-i)
for
i
in
range
(
k
):
# invariant: non-selected at [0,n-i)
j
=
self
.
_
randbelow
(
n
-
i
)
j
=
randbelow
(
n
-
i
)
result
[
i
]
=
pool
[
j
]
result
[
i
]
=
pool
[
j
]
pool
[
j
]
=
pool
[
n
-
i
-
1
]
# move non-selected item into vacancy
pool
[
j
]
=
pool
[
n
-
i
-
1
]
# move non-selected item into vacancy
else
:
else
:
selected
=
set
()
selected
=
set
()
selected_add
=
selected
.
add
selected_add
=
selected
.
add
for
i
in
range
(
k
):
for
i
in
range
(
k
):
j
=
self
.
_
randbelow
(
n
)
j
=
randbelow
(
n
)
while
j
in
selected
:
while
j
in
selected
:
j
=
self
.
_
randbelow
(
n
)
j
=
randbelow
(
n
)
selected_add
(
j
)
selected_add
(
j
)
result
[
i
]
=
population
[
j
]
result
[
i
]
=
population
[
j
]
return
result
return
result
...
...
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