Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to generate random numbers with sequence in R

Tags:

r

I need to generate random numbers between 20 and 50 with 5 increments. For example, the numbers must be 20, 25, 30, 25, 50, 45 etc. The difference between numbers should be 5.

I tried this:

x<-floor(runif(50,20,50))

this gives me any number between 20 and 50. Is there an easy way to do this in R?

like image 468
user1471980 Avatar asked May 04 '13 21:05

user1471980


People also ask

How do you generate a random number from a uniform distribution in R?

The runif() function generates random deviates of the uniform distribution and is written as runif(n, min = 0, max = 1) .

How do I generate a random number from T-distribution in R?

The R software provides access to the t-distribution by the dt() , pt() , qt() and rt() functions. Apply the help() function on these functions for further information. The rt() function generates random deviates of the t-distribution and is written as rt(n, df) . We may easily generate n number of random samples.

Does R have a random number generator?

There are in-built functions in R to generate a set of random numbers from standard distributions like normal, uniform, binomial distributions, etc.

How do I generate a random vector in R?

To create a random vector for a range of values, we can use sample function. We just need to pass the range and the sample size inside the sample function.


1 Answers

I think Arun was referring to this:

set.seed(123)
sample(seq(from = 20, to = 50, by = 5), size = 50, replace = TRUE)
#  [1] 30 45 30 50 50 20 35 50 35 35 50 35 40 40 20 50 25 20 30 50 50 40 40 50 40
# [26] 40 35 40 30 25 50 50 40 45 20 35 45 25 30 25 20 30 30 30 25 20 25 35 25 50
like image 62
Simon O'Hanlon Avatar answered Oct 09 '22 15:10

Simon O'Hanlon