Last active
February 16, 2022 17:33
-
-
Save EoinTravers/db893db77f07d2b3d2f2bf4af48b577b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
generate_spaced_dates = function(n_samples, gap = 21, | |
total_period = 365, | |
n_iter=1e12){ | |
x = runif(n_samples, 0, total_period) %>% sort() %>% round() | |
for(i in 1:1e12){ | |
is_acceptable = min(diff(x)) >= 21 | |
if(is_acceptable) { | |
break | |
} else { | |
x = runif(n_samples, 0, total_period) %>% sort() %>% round() | |
} | |
} | |
if(i == n_iter) stop(sprintf('No solution reached in %g iterations', n_iter)) | |
message(sprintf('Solution reached in %d iterations', i)) | |
return(x) | |
} | |
dates = generate_spaced_dates(12) | |
dates | |
## [1] 13 55 84 113 137 205 226 247 279 311 334 358 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment