# Simulate from a mixture of two beta distribution [closed]

I have this distribution :

$$X \sim 0.75\mathcal{B}e(\alpha_{X_1}=1,\beta_{X_1}=3)+0.25\mathcal{B}e(\alpha_{X_2}=5,\beta_{X_2}=1.75)$$

where the density function is given from this function:

dmist2b=function(y,alpha1=1,beta1=3,p=0.25,alpha2=5,beta2=1.75) {
(1 - p) * dbeta(y, alpha1, beta1) + p * dbeta(y, alpha2, beta2)}


My problem is that I have to generate from $$X$$ but I'm having some troubles. Im'not found a r library that implements this family of distributions.

I was wondering if this function that i implemeted returns the random values :

rmist2b <- function(n,alpha1,beta1,peso,alpha2,beta2) {
v <- rbinom(n,1,peso)+1
c(alpha1, alpha2)[v] + c(beta1,beta2)[v] * rbeta(n,shape1 = 0,shape2 = 0)
}


but i'm not sure that it's correct! What should I do? I think that I should first generate from two beta distribution, but in which way should I do?

Maybe someone has some ideas! Thank you in advance!

This is really easy if you understand flow control

rmixbeta<-function(){

p = runif(1)

if(p<=0.75){
x = rbeta(1,1,3)
}
if(p>0.75){
x = rbeta(1,5,1.75)
}

return(x)

}

samps = purrr::rerun(10000, rmixbeta())
samps = as.numeric(samps)
hist(samps,probability = T)

x =seq(0,1,0.01)
lines(x, 0.75*dbeta(x,1,3) + 0.25*dbeta(x,5,1.75))