Home > Mobile >  Simulating random numbers in R using a certain condition
Simulating random numbers in R using a certain condition

Time:05-14

I would like to simulate 100 random numbers from N(0,1) in such a manner that the i-th number generated is always greater than the (i-1)-th number. I've tried using the ifelse statement in R but somehow I'm unable to build the logic correctly. Any help in this regard is truly appreciated. The following chunk of code is all that I have written so far.

x <- c()
x[1] <- rnorm(1,0,1)
for (i in 2:600){
  u <- rnorm(1,0,1)
  x[i] <- ifelse(u > x[i-1], u,  )
}

I'm unsure what to include in the "else" part of ifelse so that the first line of code repeats itself when the condition is not satisfied.

CodePudding user response:

Note that OP wants a N(0,1) but uses a rnorm distribution, also taking 100 samples between 0 and 1 and sort them is not the same as taking 100 samples where each next sample is > than the previous sample. Not sure what OP truly needs and perhaps sorting 100 samples does the job. Here a solution to use a recursive function to narrow your sample range based on your previous sample.

f <- function(n = 10, results = list(), min = 0, max = 1) {
  if(length(results) < n) {
    min <- runif(1, min, max)
    results[[length(results)   1]] <- min
    f(n = n, results = results, min = min, max = max)
  } else {
    return(unlist(results))
  }
}

Note that for a uniform distribution you pretty fast reach 1 (or close to it). So lets set digits to 12 and run this 10 times with n = 100.

options(digits = 12)

lapply(seq_along(1:10), function(x) f(n = 100, min = 0, max = 1))

results

[[1]]
  [1] 0.3763035794 0.6000777578 0.8262528116 0.9700296211 0.9913666679 0.9996844525 0.9998937758 0.9999812411 0.9999856840 0.9999861654 0.9999933300 0.9999986288 0.9999994040 0.9999999106
 [15] 0.9999999554 0.9999999880 0.9999999984 0.9999999987 0.9999999997 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [29] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [43] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [57] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [71] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [85] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [99] 1.0000000000 1.0000000000

[[2]]
  [1] 0.02994098375 0.03190658763 0.65841830147 0.77279908850 0.85463952379 0.86458999676 0.88579797210 0.96825177688 0.97142843410 0.98082927112 0.99885297594 0.99978927661 0.99993025057
 [14] 0.99997768341 0.99998968122 0.99999493133 0.99999548616 0.99999759000 0.99999966589 0.99999978582 0.99999998737 0.99999999468 0.99999999812 0.99999999837 0.99999999881 0.99999999970
 [27] 0.99999999994 0.99999999996 0.99999999999 0.99999999999 0.99999999999 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [40] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [53] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [66] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [79] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [92] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000

[[3]]
  [1] 0.05012582126 0.60869282700 0.64122532111 0.73100283674 0.93514715890 0.98150331828 0.99143943201 0.99632940844 0.99678754577 0.99920893695 0.99974896360 0.99977211185 0.99977623558
 [14] 0.99982574758 0.99999823882 0.99999898485 0.99999993349 0.99999998878 0.99999999906 0.99999999934 0.99999999985 0.99999999987 0.99999999991 0.99999999998 1.00000000000 1.00000000000
 [27] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [40] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [53] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [66] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [79] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [92] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000

[[4]]
  [1] 0.08242411539 0.68549103357 0.99507964711 0.99895930547 0.99900290443 0.99916291509 0.99961547544 0.99973250428 0.99976644330 0.99982696179 0.99988805871 0.99990665698 0.99994059897
 [14] 0.99995264483 0.99998231800 0.99999752309 0.99999784617 0.99999939587 0.99999939763 0.99999980841 0.99999989689 0.99999998110 0.99999998540 0.99999999318 0.99999999716 0.99999999936
 [27] 0.99999999948 0.99999999971 0.99999999994 0.99999999999 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [40] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [53] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [66] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [79] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [92] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000

[[5]]
  [1] 0.9489589431 0.9502262107 0.9621904673 0.9915009096 0.9922819050 0.9961853471 0.9966158317 0.9994174246 0.9999227606 0.9999583406 0.9999650678 0.9999651752 0.9999926357 0.9999975173
 [15] 0.9999991033 0.9999994742 0.9999998659 0.9999998876 0.9999998936 0.9999999208 0.9999999318 0.9999999674 0.9999999769 0.9999999998 0.9999999999 0.9999999999 1.0000000000 1.0000000000
 [29] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [43] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [57] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [71] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [85] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [99] 1.0000000000 1.0000000000

[[6]]
  [1] 0.6756327748 0.9408615662 0.9894017432 0.9975549282 0.9997305971 0.9998810010 0.9999132729 0.9999663029 0.9999804085 0.9999945567 0.9999989668 0.9999995717 0.9999999615 0.9999999831
 [15] 0.9999999981 0.9999999997 0.9999999998 0.9999999999 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [29] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [43] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [57] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [71] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [85] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [99] 1.0000000000 1.0000000000

[[7]]
  [1] 0.6853584833 0.9187204479 0.9818442845 0.9963732134 0.9975625170 0.9990660413 0.9991725474 0.9993315906 0.9997449822 0.9998977530 0.9999040805 0.9999711525 0.9999924249 0.9999926011
 [15] 0.9999982165 0.9999988625 0.9999988947 0.9999999309 0.9999999880 0.9999999902 0.9999999942 0.9999999964 0.9999999973 0.9999999993 0.9999999998 0.9999999998 1.0000000000 1.0000000000
 [29] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [43] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [57] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [71] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [85] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [99] 1.0000000000 1.0000000000

[[8]]
  [1] 0.5132126578 0.7992885937 0.8396506192 0.9965300075 0.9981025662 0.9999633141 0.9999717221 0.9999819317 0.9999881022 0.9999946297 0.9999965244 0.9999985697 0.9999998684 0.9999999260
 [15] 0.9999999825 0.9999999975 0.9999999985 0.9999999994 0.9999999994 0.9999999998 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [29] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [43] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [57] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [71] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [85] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [99] 1.0000000000 1.0000000000

[[9]]
  [1] 0.02704522223 0.13525152003 0.55190017165 0.68068883674 0.82806044788 0.94897465860 0.94908163292 0.99869805473 0.99909871568 0.99994877887 0.99998287952 0.99999655750 0.99999700961
 [14] 0.99999848922 0.99999876318 0.99999881537 0.99999984870 0.99999988387 0.99999993002 0.99999995904 0.99999999733 0.99999999917 0.99999999985 0.99999999993 0.99999999997 0.99999999999
 [27] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [40] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [53] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [66] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [79] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000
 [92] 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000 1.00000000000

[[10]]
  [1] 0.4489037553 0.6620958628 0.9319925192 0.9522966957 0.9777771783 0.9820290267 0.9951483725 0.9991525948 0.9994557003 0.9995105406 0.9996636547 0.9997711600 0.9999047851 0.9999640731
 [15] 0.9999906103 0.9999949002 0.9999978360 0.9999991323 0.9999996267 0.9999996618 0.9999999033 0.9999999356 0.9999999625 0.9999999641 0.9999999702 0.9999999762 0.9999999935 0.9999999992
 [29] 0.9999999994 0.9999999998 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [43] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [57] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [71] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [85] 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000 1.0000000000
 [99] 1.0000000000 1.0000000000

CodePudding user response:

It seems you are looking for a dynamical way to generate a sorted random sequence, rather than the combo of sort runif. Interesting topic for coding practice!


Here is one option using the recursion

f <- function(n) {
    if (n == 1) {
        return(runif(n))
    }
    v <- f(n - 1)
    c(v, runif(1, tail(v, 1), 1))
}

and you will see for example

> f(10)
 [1] 0.7754963 0.7827395 0.9266231 0.9312690 0.9806459 0.9903185 0.9959223
 [8] 0.9959959 0.9980286 0.9996151

CodePudding user response:

You are looking for the sort command. This worked just fine for me:

df <- sort(rnorm(100))

  • Related