Generate three vectors of 50 random numbers each one vector

Generate three vectors of 50 random numbers each: one vector of random normal, one of random uniform, one of exponential. Create one graphic of three histograms of these data, one over the other, using the command par(mfrow=c(3,1)). Create side-by-side (same axes) comparative boxplots of the three vectors.

Programming in R

Solution

R offers us a variety of solutions for random number generation; here\'s a quick overview of some of the options.

1.One simple solution is to use the runif function, which generates a stated number of values between two end points (but not the end points themselves!) The function uses the continuous uniform distribution, meaning that every value between the two end points has an equal probability of being sampled.

Here\'s the code to produce one of random uniform 50 values between 1 and 50, and then print them.

RandomNumbers <- runif(50, 1, 50)
RandomNumbers

2. rnorm for the normal distribution. In this case, the second number in the function is the mean and the third is the standard deviation. With this example, the code generates 50 values from a normal distribution with a mean of 25 and a standard deviation of 5.5

RandomNormal <- rnorm(50, 25, 5.5)
RandomNormal

3. Exponential

> Nsim=10^4 #number of random variables
> U=runif(Nsim)
> X=-log(U) #transforms of uniforms
> Y=rexp(Nsim) #exponentials from R
> par(mfrow=c(3,1)) #plots
> hist(X,freq=F,main=\"Exp from Uniform\")
> hist(Y,freq=F,main=\"Exp from R\")

Generate three vectors of 50 random numbers each: one vector of random normal, one of random uniform, one of exponential. Create one graphic of three histograms

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site