We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
When rdunif has an upper bound b less than the lower bound a, it outputs b n times.
rdunif
b
a
n
rdunif(10, -5, 5) # [1] -5 -5 -5 -5 -5 -5 -5 -5 -5 -5
A solution is to pass a vector of integers into sample
sample
rdunif2 <- function(n, b, a = 1) { vec <- a:b sample(vec, n, replace = TRUE) } rdunif2(10, -5, 5) # [1] -2 3 2 -4 -4 3 3 2 4 -5
I've benchmarked them and rdunif2 is slightly slower.
rdunif2
microbenchmark( rdunif(1000, 50), rdunif2(1000, 50) ) # Unit: microseconds # expr min lq mean median uq max neval cld # rdunif(1000, 50) 23.179 29.8285 35.06438 31.919 34.5780 125.010 100 a # rdunif2(1000, 50) 30.019 36.0980 42.52322 37.997 39.8975 139.069 100 b
But maybe acceptably so?
The text was updated successfully, but these errors were encountered:
Passing a vector doesn't work in general because sample(10:10) probably doesn't do what you expect.
sample(10:10)
Sorry, something went wrong.
Ah I missed that! You don't need my suggestion, but I then I suppose this would work
rdunif2 <- function(n, b, a = 1) { if(a == b) return(rep(a, n)) vec <- a:b sample(vec, n, replace = TRUE) }
Can make a pull req if you want. Apologies if I'm distracting you from the important stuff.
8f435c3
No branches or pull requests
When
rdunif
has an upper boundb
less than the lower bounda
, it outputsb
n
times.A solution is to pass a vector of integers into
sample
I've benchmarked them and
rdunif2
is slightly slower.But maybe acceptably so?
The text was updated successfully, but these errors were encountered: