-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandQuestion.py
More file actions
103 lines (91 loc) · 3.92 KB
/
randQuestion.py
File metadata and controls
103 lines (91 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#https://stackoverflow.com/questions/9755538/how-do-i-create-a-list-of-random-numbers-without-duplicates/9755612#9755612
import random, math
def extractSamplesQuick(populationSize, sampleSize, interval, leafSize = 1) :
(start, end) = interval
if ((end - start) < (sampleSize - 1)): # Ideally, should not be reachable for our usage
raise ValueError("interval ("+str(start) +","+ str(end)+") smaller than "+ str(sampleSize))
if(sampleSize <= leafSize) :
return extractSamples(populationSize, sampleSize, [interval] )
len1, len2 = int(math.floor(sampleSize/2.0)), int(math.ceil(sampleSize/2.0))
# we have (len1 + len2) = sampleSize
offset = random.randint( (start + len1 - 1), (end - len2) )
# indices carefully chosen above to tally with statement below...
iL1, iL2 = (start, offset), (offset+1, end)
samples1 = extractSamplesQuick(populationSize, len1, iL1, leafSize)
samples2 = extractSamplesQuick(populationSize, len2, iL2, leafSize)
samples = []
i1 = i2 = 0
while(i1 < len1 and i2 < len2) :
chosenLst = random.choice([0,1])
if (chosenLst == 0) :
samples.append(samples1[i1])
i1 += 1
else :
samples.append(samples2[i2])
i2 += 1
if (i1 < len1) :
samples += samples1[i1:]
else :
samples += samples2[i2:]
return samples
def extractSamples(populationSize, sampleSize, intervalLst) :
if (sampleSize > populationSize) :
raise ValueError("sampleSize = "+str(sampleSize) +", is less than populationSize (= " + str(populationSize) + ")")
samples = []
while (len(samples) < sampleSize) :
i = random.randint(0, (len(intervalLst)-1))
(a,b) = intervalLst[i]
sample = random.randint(a,b)
if (a==b) :
intervalLst.pop(i)
elif (a == sample) : # shorten beginning of interval
intervalLst[i] = (sample+1, b)
elif ( sample == b) : # shorten interval end
intervalLst[i] = (a, sample - 1)
else :
intervalLst[i] = (a, sample - 1)
intervalLst.append((sample+1, b))
samples.append(sample)
return samples
def aakExtractSamplesQuick(populationSize, sampleSize, leafSize=1):
import pdb; pdb.set_trace();
return extractSamplesQuick(populationSize, sampleSize, (0,populationSize-1), leafSize)
def aakExtractSamples(populationSize, sampleSize):
return extractSamples(populationSize, sampleSize, [(0,populationSize-1)])
# https://stackoverflow.com/a/9755612/10645311 (Greg Hewgill)
def pyRandSample(populationSize, sampleSize):
return random.sample(range(populationSize), sampleSize)
def extractSamplesInbuilt(populationSize, sampleSize):
return random.sample(range(populationSize), sampleSize)
# https://stackoverflow.com/a/38398873/10645311
# Is it incorrect? (as it solves by generating samples when sampleSize > populationSize)
# Uses yield, does solve for large values
def random_sample(count, start, stop, step=1):
def gen_random():
while True:
yield random.randrange(start, stop, step)
def gen_n_unique(source, n):
seen = set()
seenadd = seen.add
for i in (i for i in source() if i not in seen and not seenadd(i)):
yield i
if len(seen) == n:
break
return [i for i in gen_n_unique(gen_random,
min(count, int(abs(stop - start) / abs(step))))]
# https://stackoverflow.com/a/9755612/10645311
def setApproach(populationSize, sampleSize):
answer = set()
answerSize = 0
while answerSize < sampleSize:
r = random.randint(0,100)
if r not in answer:
answerSize += 1
answer.add(r)
return answerSize
# https://stackoverflow.com/a/45500580/10645311
def numPySolution(populationSize, sampleSize):
import numpy as np
a = np.linspace( 0, populationSize - 1, sampleSize )
random.shuffle(a)
print(a)