0
votes

I am a newbie in R. Now, I want to create a matrix, and then extract 20 random Numbers from each of these three uniform distributions: U(0.6,0.8), U(0.0001,0.0003), U(100,110), and place them in the first three columns of the matrix, with each column corresponding to a uniform distribution. Then 20 random Numbers are extracted from each of the two normal distributions: N(7750,0.01), N(12,0.4), and placed in the last two columns of the matrix. My program is as follows, but can only achieve uniform distribution of random numbers, cannot achieve the first three columns are uniform distribution, the last two columns are the normal distribution of random numbers, How can I change it?

input <-5 # variable input
xinput <- 20 #sampling number
range <- matrix(c(0.60,0.80,
              0.0001,0.0003,       
              100,110, 
              7700,8000, 
              10,15,
),nrow=input,ncol=2,byrow=TRUE)
range
rangeresult <- matrix(0, nrow=xinput, ncol=input)# empty matrix for latter data
rangeresult
##uniform distribution
for (i in 1:input){
  set.seed(456+i) # make results reproducible
  rangeresult[,i] <- runif(xinput,range[i,1],range[i,2])
}
2
Have a look at stats::rnorm()Florian

2 Answers

0
votes

Perhaps try this

cbind(
  u1 = runif(20L, 0.6, 0.8), 
  u2 = runif(20L, 0.0001, 0.0003), 
  u3 = runif(20L, 100, 110), 
  n1 = rnorm(20L, 7750, 0.01), 
  n2 = rnorm(20L, 12, 0.4)
)

Output

             u1           u2       u3       n1       n2
 [1,] 0.7558480 0.0002851074 101.7209 7749.988 11.75270
 [2,] 0.7807589 0.0002600877 104.9278 7749.998 11.67970
 [3,] 0.7480385 0.0001562960 109.5744 7749.979 11.84603
 [4,] 0.6283492 0.0001408027 108.9455 7749.999 12.00459
 [5,] 0.7666862 0.0002485003 106.4735 7750.002 12.58783
 [6,] 0.6354397 0.0001042544 107.0999 7749.982 12.36555
 [7,] 0.7340912 0.0002507386 109.7052 7749.994 11.75111
 [8,] 0.7220797 0.0001173221 105.7116 7749.995 11.35322
 [9,] 0.6956138 0.0001478050 104.6444 7750.004 11.68879
[10,] 0.6146491 0.0001238944 108.5946 7750.006 12.78417
[11,] 0.7436676 0.0002492057 107.6073 7750.003 11.80814
[12,] 0.7916866 0.0001927277 100.1949 7750.016 12.16362
[13,] 0.7701075 0.0002236796 103.9207 7750.007 11.82555
[14,] 0.7151522 0.0001528767 101.0997 7749.996 11.75938
[15,] 0.6866158 0.0002872521 100.7036 7750.018 11.36261
[16,] 0.6106267 0.0001278512 105.8946 7749.986 11.81682
[17,] 0.6537794 0.0002875799 104.2015 7750.007 11.56224
[18,] 0.6095022 0.0001534366 108.9352 7749.993 12.22691
[19,] 0.7156714 0.0001303851 107.7274 7749.995 12.01923
[20,] 0.6397735 0.0002706792 109.6200 7749.986 12.01927
0
votes
matrix(
  c(runif(20, .6, .8),
    runif(20, .0001, .0003),
    runif(20, 100, 110),
    rnorm(20, 7750, .01),
    rnorm(20, 12, .4)),
  ncol=5)
#>            [,1]         [,2]     [,3]     [,4]     [,5]
#>  [1,] 0.6303004 0.0002700728 102.6577 7750.008 12.10271
#>  [2,] 0.7611678 0.0001594420 106.2736 7750.001 11.95071
#>  [3,] 0.7217263 0.0002726162 105.9933 7749.993 12.16880
#>  [4,] 0.7873636 0.0001409666 109.9674 7750.016 11.58212
#>  [5,] 0.7329912 0.0002504620 105.8886 7750.005 11.62768
#>  [6,] 0.6775068 0.0002546660 109.9630 7750.000 11.75542
#>  [7,] 0.6927353 0.0001217041 105.5130 7750.004 12.46987
#>  [8,] 0.7889347 0.0001849753 105.8204 7750.002 11.96011
#>  [9,] 0.7555766 0.0001712631 104.6053 7750.013 12.77534
#> [10,] 0.6225500 0.0001441519 101.4559 7750.011 11.62323
#> [11,] 0.6004412 0.0002862156 100.7426 7750.015 12.34398
#> [12,] 0.7896445 0.0001871342 103.5566 7750.002 11.18040
#> [13,] 0.7995510 0.0002998966 101.2008 7750.005 11.79095
#> [14,] 0.7271423 0.0001385434 108.3129 7750.006 11.85577
#> [15,] 0.7990341 0.0001868429 102.3255 7749.974 12.00426
#> [16,] 0.7711383 0.0001362412 108.1071 7749.995 11.62242
#> [17,] 0.7168780 0.0001821163 103.0949 7750.021 12.35856
#> [18,] 0.7197489 0.0002015831 109.4623 7749.981 11.46613
#> [19,] 0.7006335 0.0001257633 100.9744 7750.001 12.03066
#> [20,] 0.7503335 0.0002953110 102.1582 7749.989 12.54394