[R] Generation of uniform random numbers

jim holtman jholtman at gmail.com
Fri Sep 3 16:02:27 CEST 2010


Try this:

> result <- list()  # holds the numbers for each column
> for (i in seq(ncol(rate_number))){
+     ans <- NULL
+     for (j in seq(nrow(rate_number))){
+         ans <- c(ans, runif(rate_number[j, i], range_mat[j, i],
range_mat[j + 1, i]))
+     }
+     result[[i]] <- ans
+ }
> result
[[1]]
  [1] 6.498850 6.608809 6.745953 6.440018 6.638723 7.217353 6.856071 7.075837
  [9] 6.812778 6.883610 6.943057 6.756695 6.941194 7.184845 6.920174 6.991040
 [17] 7.049783 6.996771 6.843109 7.163687 8.252700 8.441360 7.411915 8.335566
 [25] 7.866912 8.481419 8.220590 8.424399 8.079554 8.044579 8.434034 7.284997
 [33] 7.965845 8.348471 8.289097 7.966429 8.541814 7.907146 7.617196 7.356019
 [41] 7.399199 7.724408 8.027951 8.243008 7.860245 8.619314 7.690405 7.938599
 [49] 7.748592 8.226306 7.637025 7.967818 8.399466 7.376370 8.562982 7.758609
 [57] 8.509161 7.770025 7.750662 7.964527 8.588298 8.546509 7.834984 8.415981
 [65] 8.690927 7.901989 8.318772 7.849992 7.738028 8.385631 7.554038 8.316682
 [73] 7.432538 7.618233 7.464957 7.609444 7.338402 8.213432 8.564404 8.418372
 [81] 9.148654 8.977637 8.955042 9.155435 9.052467 9.077362 8.926599 8.885130
 [89] 9.246342 9.066747 8.856604 8.814686 8.989059 9.212037 9.049380 9.738085
 [97] 9.615896 9.428363 9.465737 9.324106

[[2]]
  [1]  9.006539  9.357783  9.051592  9.223142  9.320051  9.495919  9.247797
  [8]  9.242175  9.086721  9.377410  9.226948  9.255585  9.103773  9.114329
 [15]  9.297856  9.287436  9.038532  9.017770  9.321398  9.464308 10.098092
 [22] 10.060901 10.026028 10.485095 10.007642 10.182788 10.101541  9.738869
 [29]  9.758166 10.229310  9.952571  9.675127 10.246698  9.604988 10.364545
 [36] 10.114645 10.057160  9.828777  9.953131 10.000441  9.680866 10.029631
 [43]  9.575276  9.777756  9.712700  9.784790 10.395094  9.946235 10.279985
 [50] 10.380619  9.913124  9.563808  9.835487 10.223726  9.837615 10.130414
 [57] 10.340615 10.356132  9.891359  9.880494 10.395445 10.144316 10.241079
 [64] 10.105303 10.403082  9.793730  9.691260 10.386451 10.003339 10.377058
 [71]  9.689194 10.258103 10.224499 10.443725 10.047647 10.211744  9.888905
 [78]  9.600873 10.427302  9.783233 10.795287 10.555180 10.920254 10.658982
 [85] 10.891426 10.633754 10.609323 10.758398 10.634475 10.590584 10.759288
 [92] 10.781391 10.564578 10.628184 10.858968 10.980705 10.550070 10.881611
 [99] 10.973983 10.909317

[[3]]
  [1] 4.404146 4.574790 4.726678 4.726866 4.419990 4.381237 4.332727 4.411084
  [9] 4.505063 4.711984 5.005480 4.878811 4.773230 4.958928 5.177001 4.923615
 [17] 4.815721 4.937243 5.065710 4.945039 5.094814 5.094707 5.027450 4.964812
 [25] 4.976360 4.903222 5.039177 5.205185 4.821302 4.957524 5.355463 5.464375
 [33] 5.316345 5.480048 5.721479 5.630987 5.716455 5.485339 5.551794 5.492495
 [41] 5.304403 5.373863 5.499257 5.436433 5.717346 5.511993 5.408572 5.388983
 [49] 5.643770 5.601231 5.332514 5.282229 5.627353 5.560205 5.334788 5.281107
 [57] 5.304515 5.440858 5.334655 5.399326 5.346105 5.378585 5.340616 5.488657
 [65] 5.635369 5.263894 5.513655 5.690160 5.436532 5.273980 5.819314 5.910746
 [73] 5.827416 5.816114 5.860653 5.863190 5.815708 6.240782 5.913507 6.003470
 [81] 6.090721 5.799585 5.809451 5.775220 6.214627 6.086856 5.797429 5.996298
 [89] 5.980776 5.937608 6.745550 6.338175 6.656718 6.284223 6.450225 6.320572
 [97] 6.346655 6.670676 6.609957 6.383606



On Fri, Sep 3, 2010 at 6:32 AM, Sarah Sanchez <sarah_sanchez09 at yahoo.com> wrote:
>
>
>
>
> Dear R helpers
>
> I have following dataset
>
> rate_number = matrix(c(5, 15, 60, 15, 5, 0, 20, 60, 20,0, 10, 20, 40, 20, 10), nrow = 5, ncol = 3)
>
> range_mat = matrix(c(6.25, 6.75, 7.25, 8.75, 9.25, 9.75, 8.5, 9, 9.5, 10.5, 11, 11.5, 4.25, 4.75, 5.25, 5.75, 6.25, 6.75), nrow = 6, ncol = 3)
>
>> rate_number
>        [,1]   [,2]   [,3]
> [1,]    5     0      10
> [2,]   15   20      20
> [3,]   60   60      40
> [4,]   15   20      20
> [5,]    5     0      10
>
>> range_mat
>      [,1]      [,2]     [,3]
> [1,] 6.25     8.5   4.25
> [2,] 6.75     9.0   4.75
> [3,] 7.25     9.5   5.25
> [4,] 8.75   10.5   5.75
> [5,] 9.25   11.0   6.25
> [6,] 9.75   11.5 6.75
>
> My problem is to generate random numbers in line with rate_number and using the range_mat. E.g.
>
> I need to generate (5, 15, 60, 15, 5 i.e. the first column of rate_number) uniform random numbers (using 1st column of range_mat) s.t the first 5 numbers will be in the range (6.25 - 6.75), next 15 numbers should be in the range (6.75 to 7.25), next 60 numbers should be in the range (7.25 to 8.75), next 15 numbers in the range (8.75 to 9.25) and last 5 numbers in the range (9.25 to 9.75).
>
> Similarily, I need to generate (0, 20, 60, 20, 0 i.e. 2nd column of rate_number) uniform random numbers in the range (using 2nd column of range_mat) i.e. (8.5 to 9), (9 to 9.5), (9.5 to 10.5), (10.5 to 11), (11 to 11.5) respectively.
>
> I could have generated these random numbers Individually using runif, but main problem is range_number could be anything i.e. there may be 50 rates but for each rate, no of rate combination will always be 5 i.e. rate_number will always have 5 rows only and also range_mat will always have 6 rows only.
>
> I tried writing loops and even tapply etc. but just can't get through.
>
> I sincerely request you to kindly guide me.
>
> Regards
>
> Sarah
>
>
>
>
>
>
>
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
>



-- 
Jim Holtman
Cincinnati, OH
+1 513 646 9390

What is the problem that you are trying to solve?



More information about the R-help mailing list