小编nik*_*iko的帖子

runif与.Machine $ double.xmax作为边界

我想生成一个随机的真实(我猜是理性的)数字.

要做到这一点,我想使用runif(1, min = m, max = M),我的想法是设置m; M(绝对)尽可能大,以使间隔尽可能大.这让我想到了我的问题:

M <- .Machine$double.xmax
m <- -M
runif(1, m, M)
## which returns
[1] Inf
Run Code Online (Sandbox Code Playgroud)

为什么不返回一个号码?选择的间隔是否太大了?

PS

> .Machine$double.xmax
[1] 1.797693e+308
Run Code Online (Sandbox Code Playgroud)

random precision r uniform-distribution

10
推荐指数
1
解决办法
268
查看次数

循环内部与外部功能

此SO帖子中,在对各种解决方案进行基准测试时进行了讨论.请考虑以下代码

# global environment is empty - new session just started
# set up
set.seed(20181231)
n <- sample(10^3:10^4,10^3)
for_loop <- function(n) {
  out <- integer(length(n))
  for(k in 1:length(out)) {
    if((k %% 2) == 0){
      out[k] <- 0L
      next
    }
    out[k] <- 1L
    next
  }
  out
}
# benchmarking
res <- microbenchmark::microbenchmark(
  for_loop = {
    out <- integer(length(n))
    for(k in 1:length(out)) {
      if((k %% 2) == 0){
        out[k] <- 0L
        next
      }
      out[k] <- 1L
      next
    }
    out
  }, …
Run Code Online (Sandbox Code Playgroud)

r microbenchmark

1
推荐指数
1
解决办法
142
查看次数