Skip to content

Instantly share code, notes, and snippets.

@stephlocke
Created January 6, 2020 17:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stephlocke/66ec504b6f5e9f3529278464898672ae to your computer and use it in GitHub Desktop.
Save stephlocke/66ec504b6f5e9f3529278464898672ae to your computer and use it in GitHub Desktop.
Quickly allocate some groups into partitions with other groups of similar size
# Generate sample distributions
n = 1e2
t = 3e6
min_r = t/1000
max_r = t/50
c = 0
r = vector(mode="numeric")
for(x in 1:n){
k = floor(runif(1, min = pmin(min_r, t-c), max = pmin(max_r, t-c)))
k = ifelse(is.nan(k), min_r, k)
r[x] = k
c = c + k
}
sum(r)
# identify partitions
p = 10
b = floor((max(r) - min(r)) / (p-1))
partition_alloc = (r - min(r)) %/% b
partition_alloc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment