(25C) Sampling without repetition
10-28-2018, 02:18 PM (This post was last modified: 10-28-2018 04:52 PM by Albert Chan.)
Post: #4
 Albert Chan Senior Member Posts: 1,840 Joined: Jul 2018
RE: (25C) Sampling without repetition
(04-27-2017 04:07 AM)nsg Wrote:  I wonder if it is possible to improve on the algorithm and generate a sample of modest k for arbitrary N,
for example, a sample of 30 out of 10000000.

Code:
import random def randindex(k, n):     s = [n] * k             # k samples     for i in range(k):         r = random.randrange(n-i)         j = 0         while r + j >= s[j]: j = j+1         s[j+1:i+1] = s[j:i] # make room for s[j]         s[j] = r + j        # sorted insert     return s

Above code only need k random numbers, and return sorted samples.

>>> randindex(10,10) # no repeat
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

>>> random.seed(1)
>>> randindex(30,10000000)
[21060, 254459, 283474, 290410, 305901, 938595 ...
 « Next Oldest | Next Newest »

 Messages In This Thread (25C) Sampling without repetition - nsg - 04-27-2017, 04:07 AM (42S) Sampling without repetition - Thomas Klemm - 10-27-2018, 03:52 PM RE: (25C) Sampling without repetition - Dieter - 10-28-2018, 09:18 AM RE: (25C) Sampling without repetition - John Keith - 10-28-2018, 08:03 PM RE: (25C) Sampling without repetition - Albert Chan - 10-28-2018, 08:51 PM RE: (25C) Sampling without repetition - Albert Chan - 10-28-2018 02:18 PM RE: (25C) Sampling without repetition - Thomas Klemm - 10-28-2018, 03:42 PM

User(s) browsing this thread: 1 Guest(s)