Search code examples
rrandomweibull

Generating random numbers in a specific interval


I want to generate some Weibull random numbers in a given interval. For example 20 random numbers from the Weibull distribution with shape 2 and scale 30 in the interval (0, 10).

rweibull function in R produce random numbers from a Weibull distribution with given shape and scale values. Can someone please suggest a method? Thank you in advance.


Solution

  • Use the distr package. It allows to do this kind of stuff very easily.

    require(distr)
    #we create the distribution
    d<-Truncate(Weibull(shape=2,scale=30),lower=0,upper=10)
    #The d object has four slots: d,r,p,q that correspond to the [drpq] prefix of standard R distributions
    #This extracts 10 random numbers
    d@r(10)
    #get an histogram
    hist(d@r(10000))