Last active
April 22, 2020 09:47
-
-
Save klmr/7e52ffd8c0db7d6b71395ab1d91c9ae5 to your computer and use it in GitHub Desktop.
Generate N random numbers fulfilling some criteria; see https://stackoverflow.com/q/61352556/1968
This file contains hidden or 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
alpha = numeric(1e4L) | |
beta = numeric(1e4L) | |
i = 0L | |
while (i < 1e4L) { | |
a = rnorm(1L, 10, 2) | |
b = rgamma(1L, 8, 1) | |
d = a - b | |
if (d < 1) { | |
i = i + 1L | |
alpha[i] = a | |
beta[i] = b | |
} | |
} | |
r = data.frame(alpha, beta) |
This file contains hidden or 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
alpha = numeric() | |
beta = numeric() | |
i = 0L | |
chunk = 20000L | |
while (i < 10000L) { | |
a = rnorm(chunk, 10, 2) | |
b = rgamma(chunk, 8, 1) | |
d = a - b | |
valid = d < 1 | |
alpha = c(alpha, a[valid]) | |
beta = c(beta, a[valid]) | |
i = i + sum(valid) | |
chunk = max(chunk %/% 2L, 10L) | |
} | |
r = data.frame(alpha = head(alpha, 1e4L), beta = head(beta, 1e4L)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment