Data Medici
Data Medici

Reputation: 137

R: Creating a vector with a specific amount of random numbers

I was hoping someone could help point me in the right direction to create a vector in R, containing a defined amount of randomly generated numbers. I am a complete newbie to R, and I have learned that the concatenate function is used for creating vectors. However, I wish to populate the vector with 50 random numbers. I do not wish to specify a range or any other conditions for the numbers.

MyVectorObject <- c(...)

Any suggestions would be greatly appreciated!

Upvotes: 10

Views: 45539

Answers (2)

Donarus
Donarus

Reputation: 78

If we are talking about integer numbers, you want to generate number in interval <-base::.Machine$integer.max, base::.Machine$integer.max> which is for example on my computer interval <-2147483647,2147483647>


Implementation

you can use base::sample to generate positive numbers from 1 to base::.Machine$integer.max

random.pos <- function(N) { 
  int.max        <- .Machine$integer.max
  return(sample(int.max, N, replace=TRUE))
}

if you want also negative numbers, you can use

random.posneg <- function(N) {
  int.max        <- .Machine$integer.max   
  random.numbers <- sample(int.max, N, replace = TRUE)
  random.signs   <- sample(c(1,-1), N, replace=TRUE)      
  return(random.numbers * random.signs)
}

NOTE: No one from functions specified above does generate 0 (zero)

The best approach (by my opinion) is to use base::runif function.

random.runif <- function(N) {
  int.max        <- .Machine$integer.max
  int.min        <- -int.max
  return(as.integer(runif(N, int.min, int.max)))
}

This will be little bit slower then using base::sample but you get random numbers uniformly distributed with possible zero.


Benchmark

library(microbenchmark)
require(compiler)

random.runif  <- cmpfun(random.runif)
random.pos    <- cmpfun(random.pos)
random.posneg <- cmpfun(random.posneg)

N <- 500

op <- microbenchmark(
  RUNIF  = random.runif(N),
  POS    = random.pos(N),
  POSNEG = random.posneg(N),
  times  = 10000
)

print(op)
## library(ggplot2)
## boxplot(op)
## qplot(y=time, data=op, colour=expr) + scale_y_log10()

and results from the benchmark above

Unit: microseconds
   expr    min     lq      mean median     uq      max neval
  RUNIF 13.423 14.251 15.197122 14.482 14.694 2425.290 10000
    POS  4.174  5.043  5.613292  5.317  5.645 2436.909 10000
 POSNEG 11.673 12.845 13.383194 13.285 13.800   60.304 10000

Upvotes: 2

Michele Usuelli
Michele Usuelli

Reputation: 2000

It depends on which numbers you want to generate. These are some options.

x1 <- rpois(n = 50, lambda = 10)
x2 <- runif(n = 50, min = 1, max = 10)
x3 <- sample(x = c(1, 3, 5), size = 50, replace = TRUE)

Upvotes: 21

Related Questions