Skip to content

Instantly share code, notes, and snippets.

@EoinTravers
Last active February 16, 2022 17:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EoinTravers/db893db77f07d2b3d2f2bf4af48b577b to your computer and use it in GitHub Desktop.
Save EoinTravers/db893db77f07d2b3d2f2bf4af48b577b to your computer and use it in GitHub Desktop.
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