Skip to content

Commit 73a585a

Browse files
committed
Merge pull request #196 from QuantEcon/random-ref
sample_without_replacement refactored
2 parents 4e168bd + df0b009 commit 73a585a

File tree

1 file changed

+4
-6
lines changed

1 file changed

+4
-6
lines changed

quantecon/random/utilities.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,14 @@ def sample_without_replacement(n, k, num_trials=None, random_state=None):
124124

125125
# Logic taken from random.sample in the standard library
126126
result = np.empty((m, k), dtype=int)
127-
pool = np.empty((m, n), dtype=int)
127+
pool = np.empty(n, dtype=int)
128128
for i in range(m):
129129
for j in range(n):
130-
pool[i, j] = j
131-
132-
for i in range(m):
130+
pool[j] = j
133131
for j in range(k):
134132
idx = int(np.floor(r[i, j] * (n-j))) # np.floor returns a float
135-
result[i, j] = pool[i, idx]
136-
pool[i, idx] = pool[i, n-j-1]
133+
result[i, j] = pool[idx]
134+
pool[idx] = pool[n-j-1]
137135

138136
if num_trials is None:
139137
return result[0]

0 commit comments

Comments
 (0)